(t *testing.T)
| 8 | ) |
| 9 | |
| 10 | func TestHTTPResponseValidation(t *testing.T) { |
| 11 | cases := []struct { |
| 12 | Name string |
| 13 | DSL func() |
| 14 | Error string |
| 15 | }{ |
| 16 | {"empty", emptyResultEmptyResponseDSL, ""}, |
| 17 | {"non empty result", nonEmptyResultEmptyResponseDSL, ""}, |
| 18 | {"non empty response", emptyResultNonEmptyResponseDSL, ""}, |
| 19 | {"header string result", stringResultResponseWithHeadersDSL, ""}, |
| 20 | {"cookie string result", stringResultResponseWithCookiesDSL, ""}, |
| 21 | {"string result text encoding", stringResultResponseWithTextContentTypeDSL, ""}, |
| 22 | {"header object result", objectResultResponseWithHeadersDSL, ""}, |
| 23 | {"cookie object result", objectResultResponseWithCookiesDSL, ""}, |
| 24 | {"header array result", arrayResultResponseWithHeadersDSL, ""}, |
| 25 | {"cookie array result", arrayResultResponseWithCookiesDSL, `service "ArrayResultResponseWithCookies" HTTP endpoint "Method": attribute "foo" used in HTTP cookies must be a primitive type.`}, |
| 26 | {"header map result", mapResultResponseWithHeadersDSL, `service "MapResultResponseWithHeaders" HTTP endpoint "Method": attribute "foo" used in HTTP headers must be a primitive type or an array of primitive types.`}, |
| 27 | {"cookie map result", mapResultResponseWithCookiesDSL, `service "MapResultResponseWithCookies" HTTP endpoint "Method": attribute "foo" used in HTTP cookies must be a primitive type.`}, |
| 28 | {"invalid", emptyResultResponseWithHeadersDSL, `HTTP response of service "EmptyResultResponseWithHeaders" HTTP endpoint "Method": response defines headers but result is empty`}, |
| 29 | {"implicit object in header", implicitObjectResultResponseWithHeadersDSL, `service "ArrayObjectResultResponseWithHeaders" HTTP endpoint "Method": attribute "foo" used in HTTP headers must be a primitive type or an array of primitive types.`}, |
| 30 | {"array of object in header", arrayObjectResultResponseWithHeadersDSL, `service "ArrayObjectResultResponseWithHeaders" HTTP endpoint "Method": Array result is mapped to an HTTP header but is not an array of primitive types.`}, |
| 31 | {"not string or []byte", intResultResponseWithTextContentTypeDSL, `HTTP response of service "StringResultResponseWithHeaders" HTTP endpoint "Method": Result type must be String or Bytes when ContentType is 'text/plain'`}, |
| 32 | {"missing header result attribute", missingHeaderResultAttributeDSL, `HTTP response of service "MissingHeaderResultAttribute" HTTP endpoint "Method": header "bar" has no equivalent attribute in result type, use notation 'attribute_name:header_name' to identify corresponding result type attribute.`}, |
| 33 | {"missing cookie result attribute", missingCookieResultAttributeDSL, `HTTP response of service "MissingCookieResultAttribute" HTTP endpoint "Method": cookie "bar" has no equivalent attribute in result type, use notation 'attribute_name:cookie_name' to identify corresponding result type attribute. |
| 34 | service "MissingCookieResultAttribute" HTTP endpoint "Method": attribute "bar" used in HTTP cookies must be a primitive type.`}, |
| 35 | {"skip encode and gRPC", skipEncodeAndGRPCDSL, `service "SkipEncodeAndGRPC" HTTP endpoint "Method": Endpoint response cannot use SkipResponseBodyEncodeDecode and define a gRPC transport.`}, |
| 36 | } |
| 37 | for _, c := range cases { |
| 38 | t.Run(c.Name, func(t *testing.T) { |
| 39 | if c.Error == "" { |
| 40 | expr.RunDSL(t, c.DSL) |
| 41 | } else { |
| 42 | err := expr.RunInvalidDSL(t, c.DSL) |
| 43 | got := stripValidationLocations(err.Error()) |
| 44 | if got != c.Error { |
| 45 | t.Errorf("got error %q, expected %q", got, c.Error) |
| 46 | } |
| 47 | } |
| 48 | }) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | var emptyResultEmptyResponseDSL = func() { |
| 53 | Service("EmptyResultEmptyResponse", func() { |
nothing calls this directly
no test coverage detected