github.com/letsencrypt/boulder@v0.20251208.0/probs/probs_test.go (about) 1 package probs 2 3 import ( 4 "testing" 5 6 "net/http" 7 8 "github.com/letsencrypt/boulder/identifier" 9 "github.com/letsencrypt/boulder/test" 10 ) 11 12 func TestProblemDetails(t *testing.T) { 13 pd := &ProblemDetails{ 14 Type: MalformedProblem, 15 Detail: "Wat? o.O", 16 HTTPStatus: 403, 17 } 18 test.AssertEquals(t, pd.String(), "malformed :: Wat? o.O") 19 } 20 21 func TestProblemDetailsConvenience(t *testing.T) { 22 testCases := []struct { 23 pb *ProblemDetails 24 expectedType ProblemType 25 statusCode int 26 detail string 27 }{ 28 {InvalidContact("invalid email detail"), InvalidContactProblem, http.StatusBadRequest, "invalid email detail"}, 29 {Connection("connection failure detail"), ConnectionProblem, http.StatusBadRequest, "connection failure detail"}, 30 {Malformed("malformed detail"), MalformedProblem, http.StatusBadRequest, "malformed detail"}, 31 {ServerInternal("internal error detail"), ServerInternalProblem, http.StatusInternalServerError, "internal error detail"}, 32 {Unauthorized("unauthorized detail"), UnauthorizedProblem, http.StatusForbidden, "unauthorized detail"}, 33 {RateLimited("rate limited detail"), RateLimitedProblem, http.StatusTooManyRequests, "rate limited detail"}, 34 {BadNonce("bad nonce detail"), BadNonceProblem, http.StatusBadRequest, "bad nonce detail"}, 35 {TLS("TLS error detail"), TLSProblem, http.StatusBadRequest, "TLS error detail"}, 36 {RejectedIdentifier("rejected identifier detail"), RejectedIdentifierProblem, http.StatusBadRequest, "rejected identifier detail"}, 37 {AccountDoesNotExist("no account detail"), AccountDoesNotExistProblem, http.StatusBadRequest, "no account detail"}, 38 {BadRevocationReason("only reason xxx is supported"), BadRevocationReasonProblem, http.StatusBadRequest, "only reason xxx is supported"}, 39 } 40 41 for _, c := range testCases { 42 if c.pb.Type != c.expectedType { 43 t.Errorf("Incorrect problem type. Expected %s got %s", c.expectedType, c.pb.Type) 44 } 45 46 if c.pb.HTTPStatus != c.statusCode { 47 t.Errorf("Incorrect HTTP Status. Expected %d got %d", c.statusCode, c.pb.HTTPStatus) 48 } 49 50 if c.pb.Detail != c.detail { 51 t.Errorf("Incorrect detail message. Expected %s got %s", c.detail, c.pb.Detail) 52 } 53 54 if subProbLen := len(c.pb.SubProblems); subProbLen != 0 { 55 t.Errorf("Incorrect SubProblems. Expected 0, found %d", subProbLen) 56 } 57 } 58 } 59 60 // TestWithSubProblems tests that a new problem can be constructed by adding 61 // subproblems. 62 func TestWithSubProblems(t *testing.T) { 63 topProb := &ProblemDetails{ 64 Type: RateLimitedProblem, 65 Detail: "don't you think you have enough certificates already?", 66 HTTPStatus: http.StatusTooManyRequests, 67 } 68 subProbs := []SubProblemDetails{ 69 { 70 Identifier: identifier.NewDNS("example.com"), 71 ProblemDetails: ProblemDetails{ 72 Type: RateLimitedProblem, 73 Detail: "don't you think you have enough certificates already?", 74 HTTPStatus: http.StatusTooManyRequests, 75 }, 76 }, 77 { 78 Identifier: identifier.NewDNS("what about example.com"), 79 ProblemDetails: ProblemDetails{ 80 Type: MalformedProblem, 81 Detail: "try a real identifier value next time", 82 HTTPStatus: http.StatusConflict, 83 }, 84 }, 85 } 86 87 outResult := topProb.WithSubProblems(subProbs) 88 89 // The outResult should be a new, distinct problem details instance 90 test.AssertNotEquals(t, topProb, outResult) 91 // The outResult problem details should have the correct sub problems 92 test.AssertDeepEquals(t, outResult.SubProblems, subProbs) 93 // Adding another sub problem shouldn't squash the original sub problems 94 anotherSubProb := SubProblemDetails{ 95 Identifier: identifier.NewDNS("another ident"), 96 ProblemDetails: ProblemDetails{ 97 Type: RateLimitedProblem, 98 Detail: "yet another rate limit err", 99 HTTPStatus: http.StatusTooManyRequests, 100 }, 101 } 102 outResult = outResult.WithSubProblems([]SubProblemDetails{anotherSubProb}) 103 test.AssertDeepEquals(t, outResult.SubProblems, append(subProbs, anotherSubProb)) 104 }