github.com/letsencrypt/boulder@v0.20251208.0/web/send_error_test.go (about)

     1  package web
     2  
     3  import (
     4  	"errors"
     5  	"net/http/httptest"
     6  	"testing"
     7  
     8  	berrors "github.com/letsencrypt/boulder/errors"
     9  	"github.com/letsencrypt/boulder/identifier"
    10  	"github.com/letsencrypt/boulder/log"
    11  	"github.com/letsencrypt/boulder/probs"
    12  	"github.com/letsencrypt/boulder/test"
    13  )
    14  
    15  func TestSendErrorSubProblemNamespace(t *testing.T) {
    16  	rw := httptest.NewRecorder()
    17  	prob := ProblemDetailsForError((&berrors.BoulderError{
    18  		Type:   berrors.Malformed,
    19  		Detail: "bad",
    20  	}).WithSubErrors(
    21  		[]berrors.SubBoulderError{
    22  			{
    23  				Identifier: identifier.NewDNS("example.com"),
    24  				BoulderError: &berrors.BoulderError{
    25  					Type:   berrors.Malformed,
    26  					Detail: "nop",
    27  				},
    28  			},
    29  			{
    30  				Identifier: identifier.NewDNS("what about example.com"),
    31  				BoulderError: &berrors.BoulderError{
    32  					Type:   berrors.Malformed,
    33  					Detail: "nah",
    34  				},
    35  			},
    36  		}),
    37  		"dfoop",
    38  	)
    39  	SendError(log.NewMock(), rw, &RequestEvent{}, prob, errors.New("it bad"))
    40  
    41  	body := rw.Body.String()
    42  	test.AssertUnmarshaledEquals(t, body, `{
    43  		"type": "urn:ietf:params:acme:error:malformed",
    44  		"detail": "dfoop :: bad",
    45  		"status": 400,
    46  		"subproblems": [
    47  		  {
    48  			"type": "urn:ietf:params:acme:error:malformed",
    49  			"detail": "dfoop :: nop",
    50  			"status": 400,
    51  			"identifier": {
    52  			  "type": "dns",
    53  			  "value": "example.com"
    54  			}
    55  		  },
    56  		  {
    57  			"type": "urn:ietf:params:acme:error:malformed",
    58  			"detail": "dfoop :: nah",
    59  			"status": 400,
    60  			"identifier": {
    61  			  "type": "dns",
    62  			  "value": "what about example.com"
    63  			}
    64  		  }
    65  		]
    66  	  }`)
    67  }
    68  
    69  func TestSendErrorSubProbLogging(t *testing.T) {
    70  	rw := httptest.NewRecorder()
    71  	prob := ProblemDetailsForError((&berrors.BoulderError{
    72  		Type:   berrors.Malformed,
    73  		Detail: "bad",
    74  	}).WithSubErrors(
    75  		[]berrors.SubBoulderError{
    76  			{
    77  				Identifier: identifier.NewDNS("example.com"),
    78  				BoulderError: &berrors.BoulderError{
    79  					Type:   berrors.Malformed,
    80  					Detail: "nop",
    81  				},
    82  			},
    83  			{
    84  				Identifier: identifier.NewDNS("what about example.com"),
    85  				BoulderError: &berrors.BoulderError{
    86  					Type:   berrors.Malformed,
    87  					Detail: "nah",
    88  				},
    89  			},
    90  		}),
    91  		"dfoop",
    92  	)
    93  	logEvent := RequestEvent{}
    94  	SendError(log.NewMock(), rw, &logEvent, prob, errors.New("it bad"))
    95  
    96  	test.AssertEquals(t, logEvent.Error, `400 :: malformed :: dfoop :: bad ["example.com :: malformed :: dfoop :: nop", "what about example.com :: malformed :: dfoop :: nah"]`)
    97  }
    98  
    99  func TestSendErrorPausedProblemLoggingSuppression(t *testing.T) {
   100  	rw := httptest.NewRecorder()
   101  	logEvent := RequestEvent{}
   102  	SendError(log.NewMock(), rw, &logEvent, probs.Paused("I better not see any of this"), nil)
   103  
   104  	test.AssertEquals(t, logEvent.Error, "429 :: rateLimited :: account/ident pair is paused")
   105  }