From 855d11771e830deb25ecd68ef85f8885af342889 Mon Sep 17 00:00:00 2001 From: maesi Date: Mon, 2 Mar 2026 18:22:24 +0100 Subject: [PATCH 01/21] doc: update start page fix: scope CSS in DocsView.vue --- webui/src/views/DocsView.vue | 2 +- webui/src/views/Home.vue | 154 +++++++++++++++++++---------------- 2 files changed, 84 insertions(+), 72 deletions(-) diff --git a/webui/src/views/DocsView.vue b/webui/src/views/DocsView.vue index cc39c78ae..4a4cdbeb4 100644 --- a/webui/src/views/DocsView.vue +++ b/webui/src/views/DocsView.vue @@ -188,7 +188,7 @@ function isLast(breadcrumb: DocEntry[], index: number) { - \ No newline at end of file From 89a54b19915d91ab47c550325ae1423aacaed89a Mon Sep 17 00:00:00 2001 From: maesi Date: Mon, 9 Mar 2026 22:52:59 +0100 Subject: [PATCH 19/21] fix(webui): update styles --- webui/src/views/DocsView.vue | 1 - 1 file changed, 1 deletion(-) diff --git a/webui/src/views/DocsView.vue b/webui/src/views/DocsView.vue index f73231a18..b67a68ea2 100644 --- a/webui/src/views/DocsView.vue +++ b/webui/src/views/DocsView.vue @@ -440,7 +440,6 @@ pre { } .box.warning{ border-color: var(--bs-warning-border-subtle); - /* background-color: #fff3cd; */ background-color: var(--bs-warning-bg-subtle); } .box.warning .box-heading { From 8c12fcef2be0b928ed9442a3c9f7d0f7198d1e20 Mon Sep 17 00:00:00 2001 From: marle3003 Date: Mon, 9 Mar 2026 23:05:51 +0100 Subject: [PATCH 20/21] fix(openapi): fix of parsing components that have overwritten already parsed elements test: refactor helper functions --- api/handler_fileserver_test.go | 32 +- api/handler_http_test.go | 87 +++-- api/handler_schema_test.go | 30 +- providers/openapi/components.go | 31 -- providers/openapi/components_test.go | 163 ++++++++-- providers/openapi/config.go | 10 +- providers/openapi/config_test.go | 7 +- providers/openapi/content_test.go | 75 +++-- providers/openapi/context_test.go | 33 +- providers/openapi/event_test.go | 16 +- providers/openapi/example_test.go | 213 ++++++------- providers/openapi/handler_requestbody_test.go | 96 +++--- providers/openapi/handler_response_test.go | 97 +++--- providers/openapi/handler_security_test.go | 40 +-- providers/openapi/handler_test.go | 297 +++++++++-------- providers/openapi/header_test.go | 245 +++++++------- providers/openapi/media_type_test.go | 298 +++++++++--------- providers/openapi/openapitest/config.go | 12 +- providers/openapi/openapitest/operation.go | 29 +- providers/openapi/openapitest/path.go | 14 +- providers/openapi/openapitest/response.go | 38 ++- providers/openapi/operation_test.go | 157 ++++----- providers/openapi/parse_test.go | 52 +-- providers/openapi/patch_test.go | 173 +++++----- providers/openapi/path.go | 4 +- providers/openapi/path_test.go | 56 ++-- providers/openapi/response.go | 4 +- providers/openapi/response_test.go | 220 +++++++------ runtime/index_test.go | 8 +- runtime/runtime_http_search_test.go | 36 +-- runtime/runtime_http_test.go | 15 +- server/httpmanager_test.go | 2 +- server/server_http_test.go | 74 ++--- 33 files changed, 1366 insertions(+), 1298 deletions(-) diff --git a/api/handler_fileserver_test.go b/api/handler_fileserver_test.go index f554e48c1..323ee6feb 100644 --- a/api/handler_fileserver_test.go +++ b/api/handler_fileserver_test.go @@ -2,7 +2,6 @@ package api_test import ( "fmt" - "github.com/stretchr/testify/require" "mokapi/api" "mokapi/config/dynamic" "mokapi/config/static" @@ -14,6 +13,8 @@ import ( "net/url" "strings" "testing" + + "github.com/stretchr/testify/require" ) func TestHandler_FileServer(t *testing.T) { @@ -168,7 +169,7 @@ func TestOpenGraphInDashboard(t *testing.T) { app := runtime.New(cfg) app.AddHttp(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0", openapitest.WithInfo("Swagger Petstore", "1.0", "This is a sample server Petstore server."), - openapitest.WithPath("/pet/{petId}", openapitest.NewPath()), + openapitest.WithPath("/pet/{petId}"), )}, ) h := api.New(app, static.Api{Path: "/mokapi", Dashboard: true}) @@ -192,9 +193,9 @@ func TestOpenGraphInDashboard(t *testing.T) { app := runtime.New(cfg) app.AddHttp(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0", openapitest.WithInfo("Swagger Petstore", "1.0", "This is a sample server Petstore server."), - openapitest.WithPath("/pet/{petId}", openapitest.NewPath( + openapitest.WithPath("/pet/{petId}", openapitest.WithPathInfo("foo", "bar"), - )), + ), ), }) h := api.New(app, static.Api{Path: "/mokapi", Dashboard: true}) @@ -218,9 +219,9 @@ func TestOpenGraphInDashboard(t *testing.T) { app := runtime.New(cfg) app.AddHttp(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0", openapitest.WithInfo("Swagger Petstore", "1.0", "This is a sample server Petstore server."), - openapitest.WithPath("/pet/{petId}", openapitest.NewPath( + openapitest.WithPath("/pet/{petId}", openapitest.WithPathInfo("", "bar"), - ))), + )), }) h := api.New(app, static.Api{Path: "/mokapi", Dashboard: true}) try.Handler(t, @@ -243,9 +244,9 @@ func TestOpenGraphInDashboard(t *testing.T) { app := runtime.New(cfg) app.AddHttp(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0", openapitest.WithInfo("Swagger Petstore", "1.0", "This is a sample server Petstore server."), - openapitest.WithPath("/pet/{petId}", openapitest.NewPath( - openapitest.WithOperation("GET", openapitest.NewOperation()), - ))), + openapitest.WithPath("/pet/{petId}", + openapitest.WithOperation("GET"), + )), }) h := api.New(app, static.Api{Path: "/mokapi", Dashboard: true}) try.Handler(t, @@ -268,12 +269,13 @@ func TestOpenGraphInDashboard(t *testing.T) { app := runtime.New(cfg) app.AddHttp(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0", openapitest.WithInfo("Swagger Petstore", "1.0", "This is a sample server Petstore server."), - openapitest.WithPath("/pet/{petId}", openapitest.NewPath( - openapitest.WithOperation("GET", openapitest.NewOperation()), - )), - openapitest.WithPath("/pet/{petId}/foo", openapitest.NewPath( - openapitest.WithOperation("GET", openapitest.NewOperation()), - ))), + openapitest.WithPath("/pet/{petId}", + openapitest.WithOperation("GET"), + ), + openapitest.WithPath("/pet/{petId}/foo", + openapitest.WithOperation("GET"), + ), + ), }) h := api.New(app, static.Api{Path: "/mokapi", Dashboard: true}) try.Handler(t, diff --git a/api/handler_http_test.go b/api/handler_http_test.go index 756af9fcb..460636508 100644 --- a/api/handler_http_test.go +++ b/api/handler_http_test.go @@ -111,10 +111,10 @@ func TestHandler_Http(t *testing.T) { return runtimetest.NewHttpApp( openapitest.NewConfig("3.0.0", openapitest.WithInfo("foo", "", ""), - openapitest.WithPath("/foo/{bar}", openapitest.NewPath( + openapitest.WithPath("/foo/{bar}", openapitest.WithPathParam("bar", openapitest.WithParamSchema(schematest.New("string"))), - openapitest.WithOperation("get", openapitest.NewOperation()), - )), + openapitest.WithOperation("get"), + ), ), ) }, @@ -127,13 +127,13 @@ func TestHandler_Http(t *testing.T) { return runtimetest.NewHttpApp( openapitest.NewConfig("3.0.0", openapitest.WithInfo("foo", "", ""), - openapitest.WithPath("/foo/{bar}", openapitest.NewPath( - openapitest.WithOperation("get", openapitest.NewOperation( + openapitest.WithPath("/foo/{bar}", + openapitest.WithOperation("get", openapitest.WithRequestBody("foo", true, openapitest.WithRequestContent("application/json", openapitest.NewContent(openapitest.WithSchema(schematest.New("string")))), ), - )), - ))), + ), + )), ) }, requestUrl: "http://foo.api/api/services/http/foo", @@ -145,11 +145,11 @@ func TestHandler_Http(t *testing.T) { return runtimetest.NewHttpApp( openapitest.NewConfig("3.0.0", openapitest.WithInfo("foo", "", ""), - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation("get", openapitest.NewOperation( + openapitest.WithPath("/foo", + openapitest.WithOperation("get", openapitest.WithSecurity(map[string][]string{"foo": {}}), - )), - )), + ), + ), openapitest.WithComponentSecurity("foo", &openapi.ApiKeySecurityScheme{ Type: "apiKey", In: "header", @@ -168,9 +168,9 @@ func TestHandler_Http(t *testing.T) { openapitest.NewConfig("3.0.0", openapitest.WithGlobalSecurity(map[string][]string{"foo": {}}), openapitest.WithInfo("foo", "", ""), - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation("get", openapitest.NewOperation()), - )), + openapitest.WithPath("/foo", + openapitest.WithOperation("get"), + ), openapitest.WithComponentSecurity("foo", &openapi.ApiKeySecurityScheme{ Type: "apiKey", In: "header", @@ -188,20 +188,17 @@ func TestHandler_Http(t *testing.T) { return runtimetest.NewHttpApp( openapitest.NewConfig("3.0.0", openapitest.WithInfo("foo", "", ""), - openapitest.WithPath("/foo/{bar}", openapitest.NewPath( - openapitest.WithOperation("get", openapitest.NewOperation( + openapitest.WithPath("/foo/{bar}", + openapitest.WithOperation("get", openapitest.WithResponse(http.StatusOK, openapitest.WithResponseDescription("foo description"), openapitest.WithContent( - "application/json", - openapitest.NewContent( - openapitest.WithSchema(schematest.New("string")), - ), - ), + "application/json", openapitest.WithSchema(schematest.New("string"))), openapitest.WithResponseHeader("foo", "bar", schematest.New("string")), ), - )), - ))), + ), + ), + ), ) }, requestUrl: "http://foo.api/api/services/http/foo", @@ -220,8 +217,8 @@ func TestHandler_Http(t *testing.T) { }, Value: openapitest.NewPath( openapitest.WithPathInfo("foo", "bar"), - openapitest.WithOperation("get", openapitest.NewOperation( - openapitest.WithResponseRef(http.StatusOK, + openapitest.WithOperation("get", + openapitest.UseResponseRef(http.StatusOK, &openapi.ResponseRef{ Reference: dynamic.Reference{ Ref: "#/components/pathItems/foo", @@ -229,16 +226,13 @@ func TestHandler_Http(t *testing.T) { }, Value: openapitest.NewResponse(openapitest.WithResponseDescription("foo description"), openapitest.WithContent( - "application/json", - openapitest.NewContent( - openapitest.WithSchema(schematest.New("string")), - ), + "application/json", openapitest.WithSchema(schematest.New("string")), ), openapitest.WithResponseHeader("foo", "bar", schematest.New("string")), ), }, ), - )), + ), ), }), ) @@ -254,19 +248,17 @@ func TestHandler_Http(t *testing.T) { return runtimetest.NewHttpApp( openapitest.NewConfig("3.0.0", openapitest.WithInfo("foo", "", ""), - openapitest.WithPath("/foo/{bar}", openapitest.NewPath( - openapitest.WithOperation("get", openapitest.NewOperation( + openapitest.WithPath("/foo/{bar}", + openapitest.WithOperation("get", openapitest.WithResponse(http.StatusOK, openapitest.WithResponseDescription("foo description"), openapitest.WithContent( - "application/json", - openapitest.NewContent( - openapitest.WithSchema(schematest.New("string", schematest.And("number"))), - ), + "application/json", openapitest.WithSchema(schematest.New("string", schematest.And("number"))), ), ), - )), - ))), + ), + ), + ), ) }, requestUrl: "http://foo.api/api/services/http/foo", @@ -278,19 +270,16 @@ func TestHandler_Http(t *testing.T) { return runtimetest.NewHttpApp( openapitest.NewConfig("3.0.0", openapitest.WithInfo("foo", "", ""), - openapitest.WithPath("/foo/{bar}", openapitest.NewPath( - openapitest.WithOperation("get", openapitest.NewOperation( + openapitest.WithPath("/foo/{bar}", + openapitest.WithOperation("get", openapitest.WithResponse(http.StatusOK, openapitest.WithResponseDescription("foo description"), openapitest.WithContent( - "application/json", - openapitest.NewContent( - openapitest.WithSchema(schematest.New("string", schematest.WithDefault("foobar"))), - ), + "application/json", openapitest.WithSchema(schematest.New("string", schematest.WithDefault("foobar"))), ), ), )), - ))), + ), ) }, requestUrl: "http://foo.api/api/services/http/foo", @@ -302,11 +291,11 @@ func TestHandler_Http(t *testing.T) { return runtimetest.NewHttpApp( openapitest.NewConfig("3.0.0", openapitest.WithInfo("foo", "", ""), - openapitest.WithPath("/foo/{bar}", openapitest.NewPath( - openapitest.WithOperation("get", openapitest.NewOperation( + openapitest.WithPath("/foo/{bar}", + openapitest.WithOperation("get", openapitest.WithTagName("foo"), - )), - )), + ), + ), openapitest.WithTag("foo", "sum", "desc"), ), ) diff --git a/api/handler_schema_test.go b/api/handler_schema_test.go index 1a59d19bc..3f07355fb 100644 --- a/api/handler_schema_test.go +++ b/api/handler_schema_test.go @@ -35,16 +35,13 @@ func TestHandler_Schema_Example_Query(t *testing.T) { app: runtimetest.NewHttpApp(openapitest.NewConfig("3.1.0", openapitest.WithInfo("foo", "", ""), openapitest.WithPath("/foo", - openapitest.NewPath(openapitest.WithOperation("GET", - openapitest.NewOperation( - openapitest.WithResponse(200, - openapitest.WithContent("application/json", openapitest.NewContent( - openapitest.WithSchema(schematest.New("string")), - )), + openapitest.WithOperation("GET", + openapitest.WithResponse(200, + openapitest.WithContent("application/json", + openapitest.WithSchema(schematest.New("string")), ), ), ), - ), ), ), ), @@ -94,20 +91,15 @@ func TestHandler_Schema_Example_Query(t *testing.T) { app: runtimetest.NewHttpApp(openapitest.NewConfig("3.1.0", openapitest.WithInfo("foo", "", ""), openapitest.WithPath("/foo", - openapitest.NewPath(openapitest.WithOperation("GET", - openapitest.NewOperation( - openapitest.WithResponse(200, - openapitest.WithContent("application/json", openapitest.NewContent( - openapitest.WithSchema(schematest.New("string")), - )), - ), - openapitest.WithResponse(400, - openapitest.WithContent("application/json", openapitest.NewContent( - openapitest.WithSchema(schematest.New("string")), - )), + openapitest.WithOperation("GET", + openapitest.WithResponse(200, + openapitest.WithContent("application/json", + openapitest.WithSchema(schematest.New("string")), ), ), - ), + openapitest.WithResponse(400, + openapitest.WithContent("application/json", openapitest.WithSchema(schematest.New("string"))), + ), ), ), ), diff --git a/providers/openapi/components.go b/providers/openapi/components.go index 0677c9630..b45d1aeb0 100644 --- a/providers/openapi/components.go +++ b/providers/openapi/components.go @@ -19,37 +19,6 @@ type Components struct { type ComponentParameters map[string]*ParameterRef -func (c *Components) parse(config *dynamic.Config, reader dynamic.Reader) error { - if c.Schemas != nil { - for it := c.Schemas.Iter(); it.Next(); { - if err := it.Value().Parse(config, reader); err != nil { - return fmt.Errorf("parse components failed: parse schema '%s' failed: %w", it.Key(), err) - } - } - } - - if err := c.Responses.parse(config, reader); err != nil { - return fmt.Errorf("parse components failed: %w", err) - } - if err := c.RequestBodies.parse(config, reader); err != nil { - return fmt.Errorf("parse components failed: %w", err) - } - if err := c.Parameters.parse(config, reader); err != nil { - return fmt.Errorf("parse components failed: %w", err) - } - if err := c.Examples.parse(config, reader); err != nil { - return fmt.Errorf("parse components failed: %w", err) - } - if err := c.Headers.parse(config, reader); err != nil { - return fmt.Errorf("parse components failed: %w", err) - } - if err := c.PathItems.parse(config, reader); err != nil { - return fmt.Errorf("parse components failed: %w", err) - } - - return nil -} - func (p ComponentParameters) parse(config *dynamic.Config, reader dynamic.Reader) error { for name, param := range p { if err := param.Parse(config, reader); err != nil { diff --git a/providers/openapi/components_test.go b/providers/openapi/components_test.go index 4e2b326f2..752353a41 100644 --- a/providers/openapi/components_test.go +++ b/providers/openapi/components_test.go @@ -3,16 +3,17 @@ package openapi_test import ( "encoding/json" "fmt" - "github.com/stretchr/testify/require" - "gopkg.in/yaml.v3" "mokapi/config/dynamic" "mokapi/config/dynamic/dynamictest" "mokapi/providers/openapi" "mokapi/providers/openapi/openapitest" - "mokapi/providers/openapi/schema" "mokapi/providers/openapi/schema/schematest" + "net/http" "net/url" "testing" + + "github.com/stretchr/testify/require" + "gopkg.in/yaml.v3" ) func TestComponents_UnmarshalJSON(t *testing.T) { @@ -207,11 +208,27 @@ func TestComponents_Parse(t *testing.T) { return cfg, nil }) config := openapitest.NewConfig("3.0", - openapitest.WithComponentSchemaRef("foo", &schema.Schema{Ref: "foo.yml#/components/schemas/foo"}), + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodGet, + openapitest.WithResponse(http.StatusOK, + openapitest.WithContent("", + openapitest.WithSchemaRef("foo.yml#/components/schemas/foo"), + ), + ), + ), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.NoError(t, err) - require.Equal(t, "string", config.Components.Schemas.Get("foo").Type.String()) + path := config.Paths["/foo"] + require.NotNil(t, path) + require.NotNil(t, path.Value) + require.NotNil(t, path.Value.Get) + require.NotNil(t, path.Value.Get.Responses) + res := path.Value.Get.Responses.GetResponse(http.StatusOK) + require.NotNil(t, res) + require.NotNil(t, res.Content[""]) + require.Equal(t, "string", res.Content[""].Schema.Type.String()) }, }, { @@ -221,10 +238,18 @@ func TestComponents_Parse(t *testing.T) { return nil, fmt.Errorf("TESTING ERROR") }) config := openapitest.NewConfig("3.0", - openapitest.WithComponentSchemaRef("foo", &schema.Schema{Ref: "foo.yml#/components/schemas/foo"}), + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodGet, + openapitest.WithResponse(http.StatusOK, + openapitest.WithContent("", + openapitest.WithSchemaRef("foo.yml#/components/schemas/foo"), + ), + ), + ), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) - require.EqualError(t, err, "parse components failed: parse schema 'foo' failed: resolve reference 'foo.yml#/components/schemas/foo' failed: TESTING ERROR") + require.EqualError(t, err, "parse path '/foo' failed: parse operation 'GET' failed: parse response '200' failed: parse content '' failed: parse schema failed: resolve reference 'foo.yml#/components/schemas/foo' failed: TESTING ERROR") }, }, { @@ -240,12 +265,24 @@ func TestComponents_Parse(t *testing.T) { return cfg, nil }) config := openapitest.NewConfig("3.0", - openapitest.WithComponentResponseRef("foo", &openapi.ResponseRef{Reference: dynamic.Reference{Ref: "foo.yml#/components/responses/foo"}}), + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodGet, + openapitest.WithResponseRef(http.StatusOK, + "foo.yml#/components/responses/foo", + ), + ), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.NoError(t, err) - r, _ := config.Components.Responses["foo"] - require.Equal(t, "foo", r.Value.Description) + + path := config.Paths["/foo"] + require.NotNil(t, path) + require.NotNil(t, path.Value) + require.NotNil(t, path.Value.Get) + require.NotNil(t, path.Value.Get.Responses) + res := path.Value.Get.Responses.GetResponse(http.StatusOK) + require.Equal(t, "foo", res.Description) }, }, { @@ -255,10 +292,16 @@ func TestComponents_Parse(t *testing.T) { return nil, fmt.Errorf("TESTING ERROR") }) config := openapitest.NewConfig("3.0", - openapitest.WithComponentResponseRef("foo", &openapi.ResponseRef{Reference: dynamic.Reference{Ref: "foo.yml#/components/schemas/foo"}}), + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodGet, + openapitest.WithResponseRef(http.StatusOK, + "foo.yml#/components/responses/foo", + ), + ), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) - require.EqualError(t, err, "parse components failed: parse response 'foo' failed: resolve reference 'foo.yml#/components/schemas/foo' failed: TESTING ERROR") + require.EqualError(t, err, "parse path '/foo' failed: parse operation 'GET' failed: parse response '200' failed: resolve reference 'foo.yml#/components/responses/foo' failed: TESTING ERROR") }, }, { @@ -274,11 +317,20 @@ func TestComponents_Parse(t *testing.T) { return cfg, nil }) config := openapitest.NewConfig("3.0", - openapitest.WithComponentRequestBodyRef("foo", &openapi.RequestBodyRef{Reference: dynamic.Reference{Ref: "foo.yml#/components/requestBodies/foo"}}), + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodGet, + openapitest.WithRequestBodyRef("foo.yml#/components/requestBodies/foo"), + ), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.NoError(t, err) - require.Equal(t, "foo", config.Components.RequestBodies["foo"].Value.Description) + path := config.Paths["/foo"] + require.NotNil(t, path) + require.NotNil(t, path.Value) + require.NotNil(t, path.Value.Get) + require.NotNil(t, path.Value.Get.RequestBody.Value) + require.Equal(t, "foo", path.Value.Get.RequestBody.Value.Description) }, }, { @@ -288,10 +340,14 @@ func TestComponents_Parse(t *testing.T) { return nil, fmt.Errorf("TESTING ERROR") }) config := openapitest.NewConfig("3.0", - openapitest.WithComponentRequestBodyRef("foo", &openapi.RequestBodyRef{Reference: dynamic.Reference{Ref: "foo.yml#/components/requestBodies/foo"}}), + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodGet, + openapitest.WithRequestBodyRef("foo.yml#/components/requestBodies/foo"), + ), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) - require.EqualError(t, err, "parse components failed: parse request body 'foo' failed: resolve reference 'foo.yml#/components/requestBodies/foo' failed: TESTING ERROR") + require.EqualError(t, err, "parse path '/foo' failed: parse operation 'GET' failed: parse request body failed: resolve reference 'foo.yml#/components/requestBodies/foo' failed: TESTING ERROR") }, }, { @@ -307,11 +363,19 @@ func TestComponents_Parse(t *testing.T) { return cfg, nil }) config := openapitest.NewConfig("3.0", - openapitest.WithComponentParameterRef("foo", &openapi.ParameterRef{Reference: dynamic.Reference{Ref: "foo.yml#/components/parameters/foo"}}), + openapitest.WithPath("/foo", + openapitest.WithPathParamRef("foo.yml#/components/parameters/foo"), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.NoError(t, err) - require.Equal(t, "foo", config.Components.Parameters["foo"].Value.Description) + path := config.Paths["/foo"] + require.NotNil(t, path) + require.NotNil(t, path.Value) + require.NotNil(t, path.Value.Parameters) + require.Len(t, path.Value.Parameters, 1) + require.NotNil(t, path.Value.Parameters[0].Value) + require.Equal(t, "foo", path.Value.Parameters[0].Value.Description) }, }, { @@ -321,10 +385,12 @@ func TestComponents_Parse(t *testing.T) { return nil, fmt.Errorf("TESTING ERROR") }) config := openapitest.NewConfig("3.0", - openapitest.WithComponentParameterRef("foo", &openapi.ParameterRef{Reference: dynamic.Reference{Ref: "foo.yml#/components/parameters/foo"}}), + openapitest.WithPath("/foo", + openapitest.WithPathParamRef("foo.yml#/components/parameters/foo"), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) - require.EqualError(t, err, "parse components failed: parse parameter 'foo' failed: resolve reference 'foo.yml#/components/parameters/foo' failed: TESTING ERROR") + require.EqualError(t, err, "parse path '/foo' failed: parse parameter '0' failed: resolve reference 'foo.yml#/components/parameters/foo' failed: TESTING ERROR") }, }, { @@ -340,11 +406,26 @@ func TestComponents_Parse(t *testing.T) { return cfg, nil }) config := openapitest.NewConfig("3.0", - openapitest.WithComponentExampleRef("foo", &openapi.ExampleRef{Reference: dynamic.Reference{Ref: "foo.yml#/components/examples/foo"}}), + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodGet, + openapitest.WithResponse(http.StatusOK, + openapitest.WithContent("", + openapitest.WithExampleRef("example", "foo.yml#/components/examples/foo")), + ), + ), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.NoError(t, err) - require.Equal(t, "foo", config.Components.Examples["foo"].Value.Description) + path := config.Paths["/foo"] + require.NotNil(t, path) + require.NotNil(t, path.Value) + require.NotNil(t, path.Value.Get) + require.NotNil(t, path.Value.Get.Responses) + res := path.Value.Get.Responses.GetResponse(http.StatusOK) + require.NotNil(t, res) + require.NotNil(t, res.Content[""]) + require.Equal(t, "foo", res.Content[""].Examples["example"].Value.Description) }, }, { @@ -354,10 +435,18 @@ func TestComponents_Parse(t *testing.T) { return nil, fmt.Errorf("TESTING ERROR") }) config := openapitest.NewConfig("3.0", - openapitest.WithComponentExampleRef("foo", &openapi.ExampleRef{Reference: dynamic.Reference{Ref: "foo.yml#/components/parameters/foo"}}), + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodGet, + openapitest.WithResponse(http.StatusOK, + openapitest.WithContent("application/json", + openapitest.WithExampleRef("foo", "foo.yml#/components/parameters/foo"), + ), + ), + ), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) - require.EqualError(t, err, "parse components failed: parse example 'foo' failed: resolve reference 'foo.yml#/components/parameters/foo' failed: TESTING ERROR") + require.EqualError(t, err, "parse path '/foo' failed: parse operation 'GET' failed: parse response '200' failed: parse content 'application/json' failed: parse example 'foo' failed: resolve reference 'foo.yml#/components/parameters/foo' failed: TESTING ERROR") }, }, { @@ -373,11 +462,23 @@ func TestComponents_Parse(t *testing.T) { return cfg, nil }) config := openapitest.NewConfig("3.0", - openapitest.WithComponentHeaderRef("foo", &openapi.HeaderRef{Reference: dynamic.Reference{Ref: "foo.yml#/components/headers/foo"}}), + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodGet, + openapitest.WithResponse(http.StatusOK, + openapitest.WithResponseHeaderRef("foo", "foo.yml#/components/headers/foo"), + ), + ), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.NoError(t, err) - require.Equal(t, "foo", config.Components.Headers["foo"].Value.Description) + path := config.Paths["/foo"] + require.NotNil(t, path) + require.NotNil(t, path.Value) + require.NotNil(t, path.Value.Get) + require.NotNil(t, path.Value.Get.Responses) + res := path.Value.Get.Responses.GetResponse(http.StatusOK) + require.Equal(t, "foo", res.Headers["foo"].Value.Description) }, }, { @@ -387,10 +488,16 @@ func TestComponents_Parse(t *testing.T) { return nil, fmt.Errorf("TESTING ERROR") }) config := openapitest.NewConfig("3.0", - openapitest.WithComponentHeaderRef("foo", &openapi.HeaderRef{Reference: dynamic.Reference{Ref: "foo.yml#/components/headers/foo"}}), + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodGet, + openapitest.WithResponse(http.StatusOK, + openapitest.WithResponseHeaderRef("foo", "foo.yml#/components/headers/foo"), + ), + ), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) - require.EqualError(t, err, "parse components failed: parse header 'foo' failed: resolve reference 'foo.yml#/components/headers/foo' failed: TESTING ERROR") + require.EqualError(t, err, "parse path '/foo' failed: parse operation 'GET' failed: parse response '200' failed: parse header 'foo' failed: resolve reference 'foo.yml#/components/headers/foo' failed: TESTING ERROR") }, }, } diff --git a/providers/openapi/config.go b/providers/openapi/config.go index 39968695f..fc9213bb9 100644 --- a/providers/openapi/config.go +++ b/providers/openapi/config.go @@ -116,15 +116,7 @@ func (c *Config) Parse(config *dynamic.Config, reader dynamic.Reader) error { config.Scope.OpenIfNeeded(config.Info.Path()) - if err := c.Paths.parse(config, reader); err != nil { - return err - } - - if err := c.Components.parse(config, reader); err != nil { - return err - } - - return nil + return c.Paths.parse(config, reader) } func (c *Config) Patch(patch *Config) { diff --git a/providers/openapi/config_test.go b/providers/openapi/config_test.go index 25cd0d4c9..aa8b08765 100644 --- a/providers/openapi/config_test.go +++ b/providers/openapi/config_test.go @@ -2,8 +2,6 @@ package openapi_test import ( "fmt" - "github.com/stretchr/testify/require" - "gopkg.in/yaml.v3" "mokapi/config/dynamic" "mokapi/config/dynamic/dynamictest" "mokapi/media" @@ -13,6 +11,9 @@ import ( "net/http" "net/url" "testing" + + "github.com/stretchr/testify/require" + "gopkg.in/yaml.v3" ) func TestResolve(t *testing.T) { @@ -431,7 +432,7 @@ func TestConfig_Patch(t *testing.T) { name: "patch add path on empty", configs: []*openapi.Config{ {}, - openapitest.NewConfig("1.0", openapitest.WithPath("/foo", openapitest.NewPath())), + openapitest.NewConfig("1.0", openapitest.WithPath("/foo")), }, test: func(t *testing.T, result *openapi.Config) { require.Len(t, result.Paths, 1) diff --git a/providers/openapi/content_test.go b/providers/openapi/content_test.go index e9fdd3a9e..db23d31bf 100644 --- a/providers/openapi/content_test.go +++ b/providers/openapi/content_test.go @@ -2,12 +2,13 @@ package openapi_test import ( "encoding/json" - "github.com/stretchr/testify/require" - "gopkg.in/yaml.v3" "mokapi/providers/openapi" "mokapi/providers/openapi/openapitest" "mokapi/providers/openapi/schema/schematest" "testing" + + "github.com/stretchr/testify/require" + "gopkg.in/yaml.v3" ) func TestContent_UnmarshalJSON(t *testing.T) { @@ -162,20 +163,22 @@ func TestConfig_Patch_Content(t *testing.T) { { name: "add MediaType", configs: []*openapi.Config{ - openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponseRef(200, &openapi.ResponseRef{Value: &openapi.Response{}}), + openapitest.NewConfig("1.0", + openapitest.WithPath( + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200), ), ), - ))), - openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/plain", &openapi.MediaType{})), + ), + openapitest.NewConfig("1.0", + openapitest.WithPath( + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.WithContent("text/plain")), ), ), - ))), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) @@ -187,20 +190,22 @@ func TestConfig_Patch_Content(t *testing.T) { { name: "append MediaType", configs: []*openapi.Config{ - openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/plain", &openapi.MediaType{})), + openapitest.NewConfig("1.0", + openapitest.WithPath( + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.WithContent("text/plain")), ), ), - ))), - openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/html", &openapi.MediaType{})), + ), + openapitest.NewConfig("1.0", + openapitest.WithPath( + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.WithContent("text/html")), ), ), - ))), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) @@ -213,20 +218,26 @@ func TestConfig_Patch_Content(t *testing.T) { { name: "patch content", configs: []*openapi.Config{ - openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/plain", &openapi.MediaType{})), + openapitest.NewConfig("1.0", + openapitest.WithPath( + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.WithContent("text/plain")), ), ), - ))), - openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/plain", &openapi.MediaType{Schema: schematest.New("string")})), + ), + openapitest.NewConfig("1.0", + openapitest.WithPath( + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, + openapitest.WithContent("text/plain", + openapitest.WithSchema(schematest.New("string")), + ), + ), ), ), - ))), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) diff --git a/providers/openapi/context_test.go b/providers/openapi/context_test.go index a6f8c27e3..b967b0fb2 100644 --- a/providers/openapi/context_test.go +++ b/providers/openapi/context_test.go @@ -1,13 +1,14 @@ package openapi_test import ( - "github.com/brianvoe/gofakeit/v6" - "github.com/stretchr/testify/require" "mokapi/media" "mokapi/providers/openapi" "mokapi/providers/openapi/openapitest" "net/http/httptest" "testing" + + "github.com/brianvoe/gofakeit/v6" + "github.com/stretchr/testify/require" ) func TestContentTypeFromRequest(t *testing.T) { @@ -38,7 +39,7 @@ func TestContentTypeFromRequest(t *testing.T) { }, { accept: "", - response: openapitest.NewResponse(openapitest.WithContent("text/plain", openapitest.NewContent())), + response: openapitest.NewResponse(openapitest.WithContent("text/plain")), validate: func(t *testing.T, ct media.ContentType, mt *openapi.MediaType, err error) { require.NotNil(t, ct) require.NotNil(t, mt) @@ -49,7 +50,7 @@ func TestContentTypeFromRequest(t *testing.T) { }, { accept: "text/plain", - response: openapitest.NewResponse(openapitest.WithContent("text/plain", openapitest.NewContent())), + response: openapitest.NewResponse(openapitest.WithContent("text/plain")), validate: func(t *testing.T, ct media.ContentType, mt *openapi.MediaType, err error) { require.NotNil(t, ct) require.NotNil(t, mt) @@ -60,7 +61,7 @@ func TestContentTypeFromRequest(t *testing.T) { }, { accept: "*/*", - response: openapitest.NewResponse(openapitest.WithContent("text/plain", openapitest.NewContent())), + response: openapitest.NewResponse(openapitest.WithContent("text/plain")), validate: func(t *testing.T, ct media.ContentType, mt *openapi.MediaType, err error) { require.NotNil(t, ct) require.NotNil(t, mt) @@ -71,7 +72,7 @@ func TestContentTypeFromRequest(t *testing.T) { }, { accept: "text/*", - response: openapitest.NewResponse(openapitest.WithContent("text/plain", openapitest.NewContent())), + response: openapitest.NewResponse(openapitest.WithContent("text/plain")), validate: func(t *testing.T, ct media.ContentType, mt *openapi.MediaType, err error) { require.NotNil(t, ct) require.NotNil(t, mt) @@ -82,14 +83,14 @@ func TestContentTypeFromRequest(t *testing.T) { }, { accept: "image/*", - response: openapitest.NewResponse(openapitest.WithContent("text/plain", openapitest.NewContent())), + response: openapitest.NewResponse(openapitest.WithContent("text/plain")), validate: func(t *testing.T, ct media.ContentType, mt *openapi.MediaType, err error) { require.EqualError(t, err, "none of requests content type(s) are supported: \"image/*\"") }, }, { accept: "image/*", - response: openapitest.NewResponse(openapitest.WithContent("image/png", openapitest.NewContent())), + response: openapitest.NewResponse(openapitest.WithContent("image/png")), validate: func(t *testing.T, ct media.ContentType, mt *openapi.MediaType, err error) { require.NotNil(t, ct) require.NotNil(t, mt) @@ -100,7 +101,7 @@ func TestContentTypeFromRequest(t *testing.T) { }, { accept: "image/png", - response: openapitest.NewResponse(openapitest.WithContent("image/*", openapitest.NewContent())), + response: openapitest.NewResponse(openapitest.WithContent("image/*")), validate: func(t *testing.T, ct media.ContentType, mt *openapi.MediaType, err error) { require.NotNil(t, ct) require.NotNil(t, mt) @@ -110,7 +111,7 @@ func TestContentTypeFromRequest(t *testing.T) { }, { accept: "image/*", - response: openapitest.NewResponse(openapitest.WithContent("image/*", openapitest.NewContent())), + response: openapitest.NewResponse(openapitest.WithContent("image/*")), validate: func(t *testing.T, ct media.ContentType, mt *openapi.MediaType, err error) { require.NotNil(t, ct) require.NotNil(t, mt) @@ -120,7 +121,7 @@ func TestContentTypeFromRequest(t *testing.T) { }, { accept: "image/*", - response: openapitest.NewResponse(openapitest.WithContent("*/*", openapitest.NewContent())), + response: openapitest.NewResponse(openapitest.WithContent("*/*")), validate: func(t *testing.T, ct media.ContentType, mt *openapi.MediaType, err error) { require.NotNil(t, ct) require.NotNil(t, mt) @@ -130,7 +131,7 @@ func TestContentTypeFromRequest(t *testing.T) { }, { accept: "*/*", - response: openapitest.NewResponse(openapitest.WithContent("*/*", openapitest.NewContent())), + response: openapitest.NewResponse(openapitest.WithContent("*/*")), validate: func(t *testing.T, ct media.ContentType, mt *openapi.MediaType, err error) { require.NotNil(t, ct) require.NotNil(t, mt) @@ -141,7 +142,7 @@ func TestContentTypeFromRequest(t *testing.T) { }, { accept: "", - response: openapitest.NewResponse(openapitest.WithContent("*/*", openapitest.NewContent())), + response: openapitest.NewResponse(openapitest.WithContent("*/*")), validate: func(t *testing.T, ct media.ContentType, mt *openapi.MediaType, err error) { require.NotNil(t, ct) require.NotNil(t, mt) @@ -152,7 +153,7 @@ func TestContentTypeFromRequest(t *testing.T) { }, { accept: "image/png", - response: openapitest.NewResponse(openapitest.WithContent("*/*", openapitest.NewContent())), + response: openapitest.NewResponse(openapitest.WithContent("*/*")), validate: func(t *testing.T, ct media.ContentType, mt *openapi.MediaType, err error) { require.NotNil(t, ct) require.NotNil(t, mt) @@ -162,7 +163,7 @@ func TestContentTypeFromRequest(t *testing.T) { }, { accept: "application/json;odata=verbose", - response: openapitest.NewResponse(openapitest.WithContent("application/json", openapitest.NewContent())), + response: openapitest.NewResponse(openapitest.WithContent("application/json")), validate: func(t *testing.T, ct media.ContentType, mt *openapi.MediaType, err error) { require.NotNil(t, ct) require.NotNil(t, mt) @@ -172,7 +173,7 @@ func TestContentTypeFromRequest(t *testing.T) { }, { accept: "application/json", - response: openapitest.NewResponse(openapitest.WithContent("application/json;odata=verbose", openapitest.NewContent())), + response: openapitest.NewResponse(openapitest.WithContent("application/json;odata=verbose")), validate: func(t *testing.T, ct media.ContentType, mt *openapi.MediaType, err error) { require.NotNil(t, ct) require.NotNil(t, mt) diff --git a/providers/openapi/event_test.go b/providers/openapi/event_test.go index d2797356f..18f957444 100644 --- a/providers/openapi/event_test.go +++ b/providers/openapi/event_test.go @@ -23,9 +23,9 @@ func TestEvent(t *testing.T) { { name: "use response example (deprecated)", config: openapitest.NewConfig("3.1.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation("GET", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent( + openapitest.WithPath("/foo", + openapitest.WithOperation("GET", + openapitest.WithResponse(200, openapitest.UseContent( "application/json", &openapi.MediaType{ Schema: nil, Example: &openapi.ExampleValue{Value: "foo"}, @@ -34,7 +34,6 @@ func TestEvent(t *testing.T) { }, )), )), - )), ), test: func(t *testing.T, h openapi.Handler) { r := httptest.NewRequest("get", "http://localhost/foo", nil) @@ -43,16 +42,16 @@ func TestEvent(t *testing.T) { err := h.ServeHTTP(rr, r) require.Nil(t, err) - require.Equal(t, http.StatusOK, rr.Code) + require.Equal(t, http.StatusOK, rr.Code, rr.Body.String()) require.Equal(t, `"foo"`, rr.Body.String()) }, }, { name: "use response examples", config: openapitest.NewConfig("3.1.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation("GET", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent( + openapitest.WithPath("/foo", + openapitest.WithOperation("GET", + openapitest.WithResponse(200, openapitest.UseContent( "application/json", &openapi.MediaType{ Schema: nil, Example: nil, @@ -71,7 +70,6 @@ func TestEvent(t *testing.T) { }, )), )), - )), ), test: func(t *testing.T, h openapi.Handler) { r := httptest.NewRequest("get", "http://localhost/foo", nil) diff --git a/providers/openapi/example_test.go b/providers/openapi/example_test.go index ceeabe722..be4f329d9 100644 --- a/providers/openapi/example_test.go +++ b/providers/openapi/example_test.go @@ -3,8 +3,6 @@ package openapi_test import ( "encoding/json" "fmt" - "github.com/stretchr/testify/require" - "gopkg.in/yaml.v3" "mokapi/config/dynamic" "mokapi/config/dynamic/dynamictest" "mokapi/providers/openapi" @@ -12,6 +10,9 @@ import ( "net/http" "net/url" "testing" + + "github.com/stretchr/testify/require" + "gopkg.in/yaml.v3" ) func TestExamples_UnmarshalJSON(t *testing.T) { @@ -154,13 +155,12 @@ func TestExample_Parse(t *testing.T) { return nil, nil }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation(http.MethodGet, openapitest.NewOperation( + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodGet, openapitest.WithResponse(http.StatusOK, - openapitest.WithContent("application/json", + openapitest.UseContent("application/json", &openapi.MediaType{Examples: map[string]*openapi.ExampleRef{"foo": nil}}), ))), - )), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.NoError(t, err) @@ -173,13 +173,12 @@ func TestExample_Parse(t *testing.T) { return nil, nil }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation(http.MethodGet, openapitest.NewOperation( + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodGet, openapitest.WithResponse(http.StatusOK, - openapitest.WithContent("application/json", + openapitest.UseContent("application/json", &openapi.MediaType{Examples: map[string]*openapi.ExampleRef{"foo": {}}}), ))), - )), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.NoError(t, err) @@ -192,14 +191,15 @@ func TestExample_Parse(t *testing.T) { return nil, fmt.Errorf("TEST ERROR") }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation(http.MethodGet, openapitest.NewOperation( + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodGet, openapitest.WithResponse(http.StatusOK, - openapitest.WithContent("application/json", &openapi.MediaType{ + openapitest.UseContent("application/json", &openapi.MediaType{ Examples: map[string]*openapi.ExampleRef{"foo": {Reference: dynamic.Reference{Ref: "foo.yml"}}}, }), - ))), - )), + ), + ), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.EqualError(t, err, "parse path '/foo' failed: parse operation 'GET' failed: parse response '200' failed: parse content 'application/json' failed: parse example 'foo' failed: resolve reference 'foo.yml' failed: TEST ERROR") @@ -216,14 +216,15 @@ func TestExample_Parse(t *testing.T) { return cfg, nil }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation(http.MethodGet, openapitest.NewOperation( + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodGet, openapitest.WithResponse(http.StatusOK, - openapitest.WithContent("application/json", &openapi.MediaType{ + openapitest.UseContent("application/json", &openapi.MediaType{ Examples: map[string]*openapi.ExampleRef{"foo": {Value: &openapi.Example{ExternalValue: "https://foo.bar"}}}, }), - ))), - )), + ), + ), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.NoError(t, err) @@ -254,28 +255,28 @@ func TestConfig_Patch_Example(t *testing.T) { name: "patch Examples", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/plain", &openapi.MediaType{ - Examples: map[string]*openapi.ExampleRef{"foo": {Value: &openapi.Example{}}}, - })), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.UseContent("text/plain", &openapi.MediaType{ + Examples: map[string]*openapi.ExampleRef{"foo": {Value: &openapi.Example{}}}, + })), ), - ))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/plain", &openapi.MediaType{ - Examples: map[string]*openapi.ExampleRef{"foo": {Value: &openapi.Example{ - Summary: "foo summary", - Description: "foo description", - Value: "foo", - ExternalValue: "https://foo.bar", - }}}, - })), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.UseContent("text/plain", &openapi.MediaType{ + Examples: map[string]*openapi.ExampleRef{"foo": {Value: &openapi.Example{ + Summary: "foo summary", + Description: "foo description", + Value: "foo", + ExternalValue: "https://foo.bar", + }}}, + })), ), - ))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) @@ -291,28 +292,28 @@ func TestConfig_Patch_Example(t *testing.T) { name: "source Examples is nil", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/plain", &openapi.MediaType{ - Examples: map[string]*openapi.ExampleRef{"foo": nil}, - })), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.UseContent("text/plain", &openapi.MediaType{ + Examples: map[string]*openapi.ExampleRef{"foo": nil}, + })), ), - ))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/plain", &openapi.MediaType{ - Examples: map[string]*openapi.ExampleRef{"foo": {Value: &openapi.Example{ - Summary: "foo summary", - Description: "foo description", - Value: "foo", - ExternalValue: "https://foo.bar", - }}}, - })), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.UseContent("text/plain", &openapi.MediaType{ + Examples: map[string]*openapi.ExampleRef{"foo": {Value: &openapi.Example{ + Summary: "foo summary", + Description: "foo description", + Value: "foo", + ExternalValue: "https://foo.bar", + }}}, + })), ), - ))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) @@ -325,25 +326,25 @@ func TestConfig_Patch_Example(t *testing.T) { name: "source Examples value is nil", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/plain", &openapi.MediaType{ - Examples: map[string]*openapi.ExampleRef{"foo": {}}, - })), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.UseContent("text/plain", &openapi.MediaType{ + Examples: map[string]*openapi.ExampleRef{"foo": {}}, + })), ), - ))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/plain", &openapi.MediaType{ - Examples: map[string]*openapi.ExampleRef{"foo": {Value: &openapi.Example{ - Summary: "foo summary", - }}}, - })), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.UseContent("text/plain", &openapi.MediaType{ + Examples: map[string]*openapi.ExampleRef{"foo": {Value: &openapi.Example{ + Summary: "foo summary", + }}}, + })), ), - ))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) @@ -356,25 +357,25 @@ func TestConfig_Patch_Example(t *testing.T) { name: "patch Examples is nil", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/plain", &openapi.MediaType{ - Examples: map[string]*openapi.ExampleRef{"foo": {Value: &openapi.Example{ - Summary: "foo summary", - }}}, - })), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.UseContent("text/plain", &openapi.MediaType{ + Examples: map[string]*openapi.ExampleRef{"foo": {Value: &openapi.Example{ + Summary: "foo summary", + }}}, + })), ), - ))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/plain", &openapi.MediaType{ - Examples: map[string]*openapi.ExampleRef{"foo": nil}, - })), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.UseContent("text/plain", &openapi.MediaType{ + Examples: map[string]*openapi.ExampleRef{"foo": nil}, + })), ), - ))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) @@ -387,25 +388,25 @@ func TestConfig_Patch_Example(t *testing.T) { name: "patch Examples value is nil", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/plain", &openapi.MediaType{ - Examples: map[string]*openapi.ExampleRef{"foo": {Value: &openapi.Example{ - Summary: "foo summary", - }}}, - })), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.UseContent("text/plain", &openapi.MediaType{ + Examples: map[string]*openapi.ExampleRef{"foo": {Value: &openapi.Example{ + Summary: "foo summary", + }}}, + })), ), - ))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/plain", &openapi.MediaType{ - Examples: map[string]*openapi.ExampleRef{"foo": {}}, - })), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.UseContent("text/plain", &openapi.MediaType{ + Examples: map[string]*openapi.ExampleRef{"foo": {}}, + })), ), - ))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) diff --git a/providers/openapi/handler_requestbody_test.go b/providers/openapi/handler_requestbody_test.go index 04a1ece39..d233a1183 100644 --- a/providers/openapi/handler_requestbody_test.go +++ b/providers/openapi/handler_requestbody_test.go @@ -29,15 +29,14 @@ func TestResponseHandler_ServeHTTP_ResponseBody(t *testing.T) { name: "text/plain", config: openapitest.NewConfig("3.0.0", openapitest.WithServer("http://localhost", ""), - openapitest.WithPath("/foo", openapitest.NewPath( + openapitest.WithPath("/foo", openapitest.WithOperation("post", - openapitest.NewOperation( - openapitest.WithRequestBody("", false, - openapitest.WithRequestContent( - "text/plain", openapitest.NewContent(openapitest.WithSchema(schematest.New("string"))))), - openapitest.WithResponse(200), - )), - )), + openapitest.WithRequestBody("", false, + openapitest.WithRequestContent( + "text/plain", openapitest.NewContent(openapitest.WithSchema(schematest.New("string"))))), + openapitest.WithResponse(200), + ), + ), ), fn: func(t *testing.T, handler openapi.Handler) { r := httptest.NewRequest("post", "http://localhost/foo", strings.NewReader("foo")) @@ -57,15 +56,14 @@ func TestResponseHandler_ServeHTTP_ResponseBody(t *testing.T) { name: "text/*", config: openapitest.NewConfig("3.0.0", openapitest.WithServer("http://localhost", ""), - openapitest.WithPath("/foo", openapitest.NewPath( + openapitest.WithPath("/foo", openapitest.WithOperation("post", - openapitest.NewOperation( - openapitest.WithRequestBody("", false, - openapitest.WithRequestContent( - "text/*", openapitest.NewContent(openapitest.WithSchema(schematest.New("string"))))), - openapitest.WithResponse(200), - )), - )), + openapitest.WithRequestBody("", false, + openapitest.WithRequestContent( + "text/*", openapitest.NewContent(openapitest.WithSchema(schematest.New("string"))))), + openapitest.WithResponse(200), + ), + ), ), fn: func(t *testing.T, handler openapi.Handler) { r := httptest.NewRequest("post", "http://localhost/foo", strings.NewReader("foo")) @@ -85,17 +83,16 @@ func TestResponseHandler_ServeHTTP_ResponseBody(t *testing.T) { name: "text/* > */*", config: openapitest.NewConfig("3.0.0", openapitest.WithServer("http://localhost", ""), - openapitest.WithPath("/foo", openapitest.NewPath( + openapitest.WithPath("/foo", openapitest.WithOperation("post", - openapitest.NewOperation( - openapitest.WithRequestBody("", false, - openapitest.WithRequestContent( - "*/*", openapitest.NewContent(openapitest.WithSchema(schematest.New("number")))), - openapitest.WithRequestContent( - "text/*", openapitest.NewContent(openapitest.WithSchema(schematest.New("string"))))), - openapitest.WithResponse(200), - )), - )), + openapitest.WithRequestBody("", false, + openapitest.WithRequestContent( + "*/*", openapitest.NewContent(openapitest.WithSchema(schematest.New("number")))), + openapitest.WithRequestContent( + "text/*", openapitest.NewContent(openapitest.WithSchema(schematest.New("string"))))), + openapitest.WithResponse(200), + ), + ), ), fn: func(t *testing.T, handler openapi.Handler) { r := httptest.NewRequest("post", "http://localhost/foo", strings.NewReader("foo")) @@ -115,17 +112,16 @@ func TestResponseHandler_ServeHTTP_ResponseBody(t *testing.T) { name: "application/json free-form", config: openapitest.NewConfig("3.0.0", openapitest.WithServer("http://localhost", ""), - openapitest.WithPath("/foo", openapitest.NewPath( + openapitest.WithPath("/foo", openapitest.WithOperation("post", - openapitest.NewOperation( - openapitest.WithRequestBody("", false, - openapitest.WithRequestContent( - "application/json", openapitest.NewContent(openapitest.WithSchema( - schematest.New("object"), - )))), - openapitest.WithResponse(200), - )), - )), + openapitest.WithRequestBody("", false, + openapitest.WithRequestContent( + "application/json", openapitest.NewContent(openapitest.WithSchema( + schematest.New("object"), + )))), + openapitest.WithResponse(200), + ), + ), ), fn: func(t *testing.T, handler openapi.Handler) { r := httptest.NewRequest("post", "http://localhost/foo", strings.NewReader(`{"foo": "abc","bar": 12}`)) @@ -145,17 +141,16 @@ func TestResponseHandler_ServeHTTP_ResponseBody(t *testing.T) { name: "application/json free-form", config: openapitest.NewConfig("3.0.0", openapitest.WithServer("http://localhost", ""), - openapitest.WithPath("/foo", openapitest.NewPath( + openapitest.WithPath("/foo", openapitest.WithOperation("post", - openapitest.NewOperation( - openapitest.WithRequestBody("", false, - openapitest.WithRequestContent( - "application/json", openapitest.NewContent(openapitest.WithSchema( - schematest.New("object", schematest.WithProperty("foo", schematest.New("string"))), - )))), - openapitest.WithResponse(200), - )), - )), + openapitest.WithRequestBody("", false, + openapitest.WithRequestContent( + "application/json", openapitest.NewContent(openapitest.WithSchema( + schematest.New("object", schematest.WithProperty("foo", schematest.New("string"))), + )))), + openapitest.WithResponse(200), + ), + ), ), fn: func(t *testing.T, handler openapi.Handler) { r := httptest.NewRequest("post", "http://localhost/foo", strings.NewReader(`{"foo": "abc","bar": 12}`)) @@ -174,12 +169,11 @@ func TestResponseHandler_ServeHTTP_ResponseBody(t *testing.T) { name: "no request body specified but request contains body", config: openapitest.NewConfig("3.0.0", openapitest.WithServer("http://localhost", ""), - openapitest.WithPath("/foo", openapitest.NewPath( + openapitest.WithPath("/foo", openapitest.WithOperation("post", - openapitest.NewOperation( - openapitest.WithResponse(200), - )), - )), + openapitest.WithResponse(200), + ), + ), ), fn: func(t *testing.T, handler openapi.Handler) { spy := &spyBody{Reader: bytes.NewBufferString(`{"foo": "abc","bar": 12}`)} diff --git a/providers/openapi/handler_response_test.go b/providers/openapi/handler_response_test.go index 2a7ea6e6f..ea9e9adf2 100644 --- a/providers/openapi/handler_response_test.go +++ b/providers/openapi/handler_response_test.go @@ -21,16 +21,12 @@ func TestHandler_Response(t *testing.T) { var opts []openapitest.ResponseOptions if contentType != "" { opts = append(opts, openapitest.WithContent(contentType, - openapitest.NewContent(openapitest.WithSchema(s))), + openapitest.WithSchema(s)), ) } - op := openapitest.NewOperation( - openapitest.WithResponse(http.StatusOK, opts...), - ) - return openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath(openapitest.WithOperation(http.MethodGet, op)))) + openapitest.WithPath("/foo", openapitest.WithOperation(http.MethodGet, openapitest.WithResponse(http.StatusOK, opts...)))) } testcases := []struct { @@ -206,14 +202,15 @@ func TestHandler_Response_Context(t *testing.T) { { name: "use query data in random response", opt: openapitest.WithPath("/foo", - openapitest.NewPath(openapitest.WithOperation(http.MethodGet, openapitest.NewOperation( + openapitest.WithOperation(http.MethodGet, openapitest.WithQueryParam("name", false, openapitest.WithParamSchema(schematest.New("string"))), openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", - openapitest.NewContent(openapitest.WithSchema( + openapitest.WithSchema( schematest.New("object", schematest.WithProperty("name", schematest.New("string")))), - ), - )), - )))), + ), + ), + ), + ), req: func() *http.Request { return httptest.NewRequest("get", "http://localhost/foo?name=foo", nil) }, @@ -225,15 +222,14 @@ func TestHandler_Response_Context(t *testing.T) { { name: "use from path parameter", opt: openapitest.WithPath("/users/{id}", - openapitest.NewPath( - openapitest.WithPathParam("id", openapitest.WithParamSchema(schematest.New("integer"))), - openapitest.WithOperation(http.MethodGet, openapitest.NewOperation( - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", - openapitest.NewContent(openapitest.WithSchema( - schematest.New("object", schematest.WithProperty("id", schematest.New("integer")))), - ), - )), - ))), + openapitest.WithPathParam("id", openapitest.WithParamSchema(schematest.New("integer"))), + openapitest.WithOperation(http.MethodGet, + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", + openapitest.WithSchema( + schematest.New("object", schematest.WithProperty("id", schematest.New("integer")))), + ), + ), + ), ), req: func() *http.Request { return httptest.NewRequest("get", "http://localhost/users/123", nil) @@ -246,19 +242,18 @@ func TestHandler_Response_Context(t *testing.T) { { name: "parameter does not match response type", opt: openapitest.WithPath("/users/{id}", - openapitest.NewPath( - openapitest.WithPathParam("id", openapitest.WithParamSchema(schematest.New("integer"))), - openapitest.WithOperation(http.MethodGet, openapitest.NewOperation( - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", - openapitest.NewContent(openapitest.WithSchema( - schematest.New("object", - schematest.WithProperty("id", schematest.New("string")), - schematest.WithRequired("id"), - ), - ), + openapitest.WithPathParam("id", openapitest.WithParamSchema(schematest.New("integer"))), + openapitest.WithOperation(http.MethodGet, + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", + openapitest.WithSchema( + schematest.New("object", + schematest.WithProperty("id", schematest.New("string")), + schematest.WithRequired("id"), ), - )), - ))), + ), + ), + ), + ), ), req: func() *http.Request { return httptest.NewRequest("get", "http://localhost/users/123", nil) @@ -271,15 +266,14 @@ func TestHandler_Response_Context(t *testing.T) { { name: "parameter does not match response type but string to int", opt: openapitest.WithPath("/users/{id}", - openapitest.NewPath( - openapitest.WithPathParam("id", openapitest.WithParamSchema(schematest.New("string"))), - openapitest.WithOperation(http.MethodGet, openapitest.NewOperation( - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", - openapitest.NewContent(openapitest.WithSchema( - schematest.New("object", schematest.WithProperty("id", schematest.New("integer")))), - ), - )), - ))), + openapitest.WithPathParam("id", openapitest.WithParamSchema(schematest.New("string"))), + openapitest.WithOperation(http.MethodGet, + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", + openapitest.WithSchema( + schematest.New("object", schematest.WithProperty("id", schematest.New("integer")))), + ), + ), + ), ), req: func() *http.Request { return httptest.NewRequest("get", "http://localhost/users/123", nil) @@ -292,10 +286,9 @@ func TestHandler_Response_Context(t *testing.T) { { name: "response reference is nil", opt: openapitest.WithPath("/users", - openapitest.NewPath( - openapitest.WithOperation(http.MethodGet, openapitest.NewOperation( - openapitest.WithResponseRef(http.StatusOK, nil), - ))), + openapitest.WithOperation(http.MethodGet, + openapitest.UseResponse(http.StatusOK, nil), + ), ), req: func() *http.Request { return httptest.NewRequest("get", "http://localhost/users", nil) @@ -308,22 +301,22 @@ func TestHandler_Response_Context(t *testing.T) { { name: "from context and parameter is array ", opt: openapitest.WithPath("/foo", - openapitest.NewPath(openapitest.WithOperation(http.MethodGet, openapitest.NewOperation( + openapitest.WithOperation(http.MethodGet, openapitest.WithQueryParam( "name", false, openapitest.WithParamSchema(schematest.New("array", schematest.WithItems("string"))), openapitest.WithExplode(true)), openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", - openapitest.NewContent( - openapitest.WithSchema( - schematest.New("array", schematest.WithItems( - "object", schematest.WithProperty("name", schematest.New("string"))), - ), + openapitest.WithSchema( + schematest.New("array", schematest.WithItems( + "object", schematest.WithProperty("name", schematest.New("string"))), ), ), ), - ))))), + ), + ), + ), req: func() *http.Request { return httptest.NewRequest("get", "http://localhost/foo?name=foo&name=bar", nil) }, diff --git a/providers/openapi/handler_security_test.go b/providers/openapi/handler_security_test.go index 5133a61f9..a1eeffd17 100644 --- a/providers/openapi/handler_security_test.go +++ b/providers/openapi/handler_security_test.go @@ -24,7 +24,7 @@ func TestHandler_Security(t *testing.T) { test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config, eh events.Handler) { op := openapitest.NewOperation( openapitest.WithSecurity(map[string][]string{"foo": {}}), - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent())), + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json")), ) c.Components.SecuritySchemes = map[string]openapi.SecurityScheme{ "foo": &openapi.HttpSecurityScheme{ @@ -32,7 +32,7 @@ func TestHandler_Security(t *testing.T) { }, } - openapitest.AppendPath("/foo", c, openapitest.WithOperation("GET", op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("GET", op)) r := httptest.NewRequest("GET", "http://localhost/foo", nil) r.Header.Set("Authorization", "Basic 123") rr := httptest.NewRecorder() @@ -56,7 +56,7 @@ func TestHandler_Security(t *testing.T) { test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config, eh events.Handler) { op := openapitest.NewOperation( openapitest.WithSecurity(map[string][]string{"foo": {}}), - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent())), + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json")), ) c.Components.SecuritySchemes = map[string]openapi.SecurityScheme{ "foo": &openapi.HttpSecurityScheme{ @@ -64,7 +64,7 @@ func TestHandler_Security(t *testing.T) { }, } - openapitest.AppendPath("/foo", c, openapitest.WithOperation("GET", op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("GET", op)) r := httptest.NewRequest("GET", "http://localhost/foo", nil) rr := httptest.NewRecorder() h(rr, r) @@ -81,7 +81,7 @@ func TestHandler_Security(t *testing.T) { test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config, eh events.Handler) { op := openapitest.NewOperation( openapitest.WithSecurity(map[string][]string{"foo": {}}), - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent())), + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json")), ) c.Components.SecuritySchemes = map[string]openapi.SecurityScheme{ "foo": &openapi.HttpSecurityScheme{ @@ -89,7 +89,7 @@ func TestHandler_Security(t *testing.T) { }, } - openapitest.AppendPath("/foo", c, openapitest.WithOperation("GET", op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("GET", op)) r := httptest.NewRequest("GET", "http://localhost/foo", nil) r.Header.Set("Authorization", "Bearer 123") rr := httptest.NewRecorder() @@ -114,7 +114,7 @@ func TestHandler_Security(t *testing.T) { test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config, eh events.Handler) { op := openapitest.NewOperation( openapitest.WithSecurity(map[string][]string{"foo": {}}), - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent())), + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json")), ) c.Components.SecuritySchemes = map[string]openapi.SecurityScheme{ "foo": &openapi.ApiKeySecurityScheme{ @@ -123,7 +123,7 @@ func TestHandler_Security(t *testing.T) { }, } - openapitest.AppendPath("/foo", c, openapitest.WithOperation("GET", op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("GET", op)) r := httptest.NewRequest("GET", "http://localhost/foo", nil) r.Header.Set("X-API-KEY", "123") rr := httptest.NewRecorder() @@ -148,7 +148,7 @@ func TestHandler_Security(t *testing.T) { test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config, eh events.Handler) { op := openapitest.NewOperation( openapitest.WithSecurity(map[string][]string{"foo": {}}), - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent())), + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json")), ) c.Components.SecuritySchemes = map[string]openapi.SecurityScheme{ "foo": &openapi.ApiKeySecurityScheme{ @@ -157,7 +157,7 @@ func TestHandler_Security(t *testing.T) { }, } - openapitest.AppendPath("/foo", c, openapitest.WithOperation("GET", op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("GET", op)) r := httptest.NewRequest("GET", "http://localhost/foo?apikey=123", nil) rr := httptest.NewRecorder() h(rr, r) @@ -182,7 +182,7 @@ func TestHandler_Security(t *testing.T) { test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config, eh events.Handler) { op := openapitest.NewOperation( openapitest.WithSecurity(map[string][]string{"foo": {}}), - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent())), + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json")), ) c.Components.SecuritySchemes = map[string]openapi.SecurityScheme{ "foo": &openapi.ApiKeySecurityScheme{ @@ -191,7 +191,7 @@ func TestHandler_Security(t *testing.T) { }, } - openapitest.AppendPath("/foo", c, openapitest.WithOperation("GET", op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("GET", op)) r := httptest.NewRequest("GET", "http://localhost/foo", nil) r.AddCookie(&http.Cookie{Name: "apikey", Value: "123"}) rr := httptest.NewRecorder() @@ -216,7 +216,7 @@ func TestHandler_Security(t *testing.T) { test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config, eh events.Handler) { op := openapitest.NewOperation( openapitest.WithSecurity(map[string][]string{"foo": {}}), - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent())), + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json")), ) c.Components.SecuritySchemes = map[string]openapi.SecurityScheme{ "foo": &openapi.NotSupportedSecurityScheme{ @@ -224,7 +224,7 @@ func TestHandler_Security(t *testing.T) { }, } - openapitest.AppendPath("/foo", c, openapitest.WithOperation("GET", op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("GET", op)) r := httptest.NewRequest("GET", "http://localhost/foo", nil) rr := httptest.NewRecorder() h(rr, r) @@ -236,12 +236,12 @@ func TestHandler_Security(t *testing.T) { test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config, eh events.Handler) { op := openapitest.NewOperation( openapitest.WithSecurity(map[string][]string{"foo": {}}), - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent())), + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json")), ) c.Components.SecuritySchemes = map[string]openapi.SecurityScheme{ "foo": &openapi.OAuth2SecurityScheme{}, } - openapitest.AppendPath("/foo", c, openapitest.WithOperation("GET", op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("GET", op)) r := httptest.NewRequest("GET", "http://localhost/foo", nil) r.Header.Set("Authorization", "Bearer 123") @@ -273,7 +273,7 @@ func TestHandler_Security(t *testing.T) { "bar": {}, }, ), - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent())), + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json")), ) c.Components.SecuritySchemes = map[string]openapi.SecurityScheme{ "foo": &openapi.OAuth2SecurityScheme{}, @@ -282,7 +282,7 @@ func TestHandler_Security(t *testing.T) { Name: "apikey", }, } - openapitest.AppendPath("/foo", c, openapitest.WithOperation("GET", op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("GET", op)) r := httptest.NewRequest("GET", "http://localhost/foo", nil) r.Header.Set("Authorization", "Bearer 123") @@ -306,7 +306,7 @@ func TestHandler_Security(t *testing.T) { op := openapitest.NewOperation( openapitest.WithSecurity(map[string][]string{"foo": {}}), openapitest.WithSecurity(map[string][]string{"bar": {}}), - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent())), + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json")), ) c.Components.SecuritySchemes = map[string]openapi.SecurityScheme{ "foo": &openapi.OAuth2SecurityScheme{}, @@ -315,7 +315,7 @@ func TestHandler_Security(t *testing.T) { Name: "apikey", }, } - openapitest.AppendPath("/foo", c, openapitest.WithOperation("GET", op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("GET", op)) r := httptest.NewRequest("GET", "http://localhost/foo", nil) r.Header.Set("apikey", "API_KEY_123") diff --git a/providers/openapi/handler_test.go b/providers/openapi/handler_test.go index 94404738b..ae4b86161 100644 --- a/providers/openapi/handler_test.go +++ b/providers/openapi/handler_test.go @@ -41,8 +41,8 @@ func TestResolveEndpoint(t *testing.T) { name: "base path", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { c.Servers[0].Url = "http://localhost/root" - op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent()))) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json"))) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/root/foo", nil) r = r.WithContext(context.WithValue(r.Context(), "servicePath", "/root")) rr := httptest.NewRecorder() @@ -55,8 +55,8 @@ func TestResolveEndpoint(t *testing.T) { name: "base path single slash", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { c.Servers[0].Url = "http://localhost/root" - op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent()))) - openapitest.AppendPath("/", c, openapitest.WithOperation("get", op)) + op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json"))) + openapitest.AppendPath("/", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/root", nil) r = r.WithContext(context.WithValue(r.Context(), "servicePath", "/root")) rr := httptest.NewRecorder() @@ -69,8 +69,8 @@ func TestResolveEndpoint(t *testing.T) { name: "server url with slash suffix", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { c.Servers[0].Url = "http://localhost/root/" - op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent()))) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json"))) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/root/foo", nil) r = r.WithContext(context.WithValue(r.Context(), "servicePath", "/root/")) rr := httptest.NewRecorder() @@ -86,11 +86,10 @@ func TestResolveEndpoint(t *testing.T) { op := openapitest.NewOperation( openapitest.WithOperationParam("bar", true), openapitest.WithResponse(http.StatusOK, - openapitest.WithContent("application/json", - openapitest.NewContent()), + openapitest.WithContent("application/json"), ), ) - openapitest.AppendPath("/foo/{bar}", c, openapitest.WithOperation("get", op)) + openapitest.AppendPath("/foo/{bar}", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/root/foo/bar", nil) r = r.WithContext(context.WithValue(r.Context(), "servicePath", "/root")) @@ -104,8 +103,8 @@ func TestResolveEndpoint(t *testing.T) { name: "spec define suffix / but request does not", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { c.Servers[0].Url = "http://localhost" - op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent()))) - openapitest.AppendPath("/foo/", c, openapitest.WithOperation("get", op)) + op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json"))) + openapitest.AppendPath("/foo/", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo", nil) rr := httptest.NewRecorder() h(rr, r) @@ -118,8 +117,8 @@ func TestResolveEndpoint(t *testing.T) { name: "spec define suffix no / but request does", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { c.Servers[0].Url = "http://localhost" - op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent()))) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json"))) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo/", nil) rr := httptest.NewRecorder() h(rr, r) @@ -132,8 +131,8 @@ func TestResolveEndpoint(t *testing.T) { name: "both uses trailing slash", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { c.Servers[0].Url = "http://localhost" - op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent()))) - openapitest.AppendPath("/foo/", c, openapitest.WithOperation("get", op)) + op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json"))) + openapitest.AppendPath("/foo/", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo/", nil) rr := httptest.NewRecorder() h(rr, r) @@ -158,8 +157,8 @@ func TestResolveEndpoint(t *testing.T) { { name: "HTTP method does not match", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { - op := openapitest.NewOperation(openapitest.WithResponse(http.StatusNotFound, openapitest.WithContent("application/json", openapitest.NewContent()))) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + op := openapitest.NewOperation(openapitest.WithResponse(http.StatusNotFound, openapitest.WithContent("application/json"))) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("POST", "http://localhost/foo", nil) rr := httptest.NewRecorder() h(rr, r) @@ -171,8 +170,8 @@ func TestResolveEndpoint(t *testing.T) { { name: "no success response specified", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { - op := openapitest.NewOperation(openapitest.WithResponse(http.StatusNotFound, openapitest.WithContent("application/json", openapitest.NewContent()))) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + op := openapitest.NewOperation(openapitest.WithResponse(http.StatusNotFound, openapitest.WithContent("application/json"))) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("GET", "http://localhost/foo", nil) rr := httptest.NewRecorder() h(rr, r) @@ -184,7 +183,7 @@ func TestResolveEndpoint(t *testing.T) { name: "no response specified", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { op := openapitest.NewOperation() - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("GET", "http://localhost/foo", nil) rr := httptest.NewRecorder() h(rr, r) @@ -196,8 +195,8 @@ func TestResolveEndpoint(t *testing.T) { name: "no success response specified but default", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { // 0 = default - op := openapitest.NewOperation(openapitest.WithResponse(0, openapitest.WithContent("application/json", openapitest.NewContent()))) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + op := openapitest.NewOperation(openapitest.WithResponse(0, openapitest.WithContent("application/json"))) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("GET", "http://localhost/foo", nil) rr := httptest.NewRecorder() h(rr, r) @@ -207,8 +206,8 @@ func TestResolveEndpoint(t *testing.T) { { name: "with endpoint", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { - op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent()))) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json"))) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo", nil) rr := httptest.NewRecorder() h(rr, r) @@ -219,9 +218,9 @@ func TestResolveEndpoint(t *testing.T) { { name: "with multiple success response 1/2", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { - op := openapitest.NewOperation(openapitest.WithResponse(http.StatusNoContent, openapitest.WithContent("application/json", openapitest.NewContent())), - openapitest.WithResponse(http.StatusAccepted, openapitest.WithContent("application/json", openapitest.NewContent()))) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + op := openapitest.NewOperation(openapitest.WithResponse(http.StatusNoContent, openapitest.WithContent("application/json")), + openapitest.WithResponse(http.StatusAccepted, openapitest.WithContent("application/json"))) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo", nil) rr := httptest.NewRecorder() h(rr, r) @@ -231,9 +230,9 @@ func TestResolveEndpoint(t *testing.T) { { name: "with multiple success response 2/2", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { - op := openapitest.NewOperation(openapitest.WithResponse(http.StatusAccepted, openapitest.WithContent("application/json", openapitest.NewContent())), - openapitest.WithResponse(http.StatusNoContent, openapitest.WithContent("application/json", openapitest.NewContent()))) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + op := openapitest.NewOperation(openapitest.WithResponse(http.StatusAccepted, openapitest.WithContent("application/json")), + openapitest.WithResponse(http.StatusNoContent, openapitest.WithContent("application/json"))) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo", nil) rr := httptest.NewRecorder() h(rr, r) @@ -244,7 +243,7 @@ func TestResolveEndpoint(t *testing.T) { name: "empty response body", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { op := openapitest.NewOperation(openapitest.WithResponse(http.StatusNoContent)) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo", nil) rr := httptest.NewRecorder() h(rr, r) @@ -257,8 +256,8 @@ func TestResolveEndpoint(t *testing.T) { { name: "POST request", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { - op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent()))) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("POST", op)) + op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json"))) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("POST", op)) r := httptest.NewRequest("POST", "http://localhost/foo", nil) rr := httptest.NewRecorder() h(rr, r) @@ -268,11 +267,11 @@ func TestResolveEndpoint(t *testing.T) { { name: "POST request invalid data", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { - op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent())), + op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json")), openapitest.WithRequestBody("", true, openapitest.WithRequestContent("application/json", openapitest.NewContent(openapitest.WithSchema(schematest.New("string", schematest.WithMinLength(4))))))) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("POST", op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("POST", op)) r := httptest.NewRequest("POST", "http://localhost/foo", strings.NewReader(`"foo"`)) r.Header.Set("Content-Type", "application/json") rr := httptest.NewRecorder() @@ -287,8 +286,8 @@ func TestResolveEndpoint(t *testing.T) { { name: "PUT request", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { - op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent()))) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("PUT", op)) + op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json"))) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("PUT", op)) r := httptest.NewRequest("PUT", "http://localhost/foo", nil) rr := httptest.NewRecorder() h(rr, r) @@ -301,8 +300,8 @@ func TestResolveEndpoint(t *testing.T) { { name: "PATCH request", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { - op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent()))) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("PATCH", op)) + op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json"))) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("PATCH", op)) r := httptest.NewRequest("PATCH", "http://localhost/foo", nil) rr := httptest.NewRecorder() h(rr, r) @@ -315,8 +314,8 @@ func TestResolveEndpoint(t *testing.T) { { name: "DELETE request", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { - op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent()))) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("DELETE", op)) + op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json"))) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("DELETE", op)) r := httptest.NewRequest("DELETE", "http://localhost/foo", nil) rr := httptest.NewRecorder() h(rr, r) @@ -329,8 +328,8 @@ func TestResolveEndpoint(t *testing.T) { { name: "HEAD request", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { - op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent()))) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("HEAD", op)) + op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json"))) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("HEAD", op)) r := httptest.NewRequest("HEAD", "http://localhost/foo", nil) rr := httptest.NewRecorder() h(rr, r) @@ -343,8 +342,8 @@ func TestResolveEndpoint(t *testing.T) { { name: "OPTIONS request", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { - op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent()))) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("OPTIONS", op)) + op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json"))) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("OPTIONS", op)) r := httptest.NewRequest("OPTIONS", "http://localhost/foo", nil) rr := httptest.NewRecorder() h(rr, r) @@ -357,8 +356,8 @@ func TestResolveEndpoint(t *testing.T) { { name: "TRACE request", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { - op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent()))) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("TRACE", op)) + op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json"))) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("TRACE", op)) r := httptest.NewRequest("TRACE", "http://localhost/foo", nil) rr := httptest.NewRecorder() h(rr, r) @@ -371,8 +370,8 @@ func TestResolveEndpoint(t *testing.T) { { name: "QUERY request", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { - op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent()))) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("QUERY", op)) + op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json"))) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("QUERY", op)) r := httptest.NewRequest("QUERY", "http://localhost/foo", nil) rr := httptest.NewRecorder() h(rr, r) @@ -385,8 +384,8 @@ func TestResolveEndpoint(t *testing.T) { { name: "custom request method", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { - op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent()))) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("LINK", op)) + op := openapitest.NewOperation(openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json"))) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("LINK", op)) r := httptest.NewRequest("LINK", "http://localhost/foo", nil) rr := httptest.NewRecorder() h(rr, r) @@ -402,7 +401,7 @@ func TestResolveEndpoint(t *testing.T) { op := openapitest.NewOperation( openapitest.WithResponse(http.StatusOK), openapitest.WithOperationParam("id", false)) - openapitest.AppendPath("/foo/{id}", c, openapitest.WithOperation("get", op)) + openapitest.AppendPath("/foo/{id}", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo", nil) rr := httptest.NewRecorder() h(rr, r) @@ -416,7 +415,7 @@ func TestResolveEndpoint(t *testing.T) { op := openapitest.NewOperation( openapitest.WithResponse(http.StatusOK), openapitest.WithOperationParam("id", false)) - openapitest.AppendPath("/foo/{id}/bar", c, openapitest.WithOperation("get", op)) + openapitest.AppendPath("/foo/{id}/bar", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo", nil) rr := httptest.NewRecorder() h(rr, r) @@ -428,9 +427,9 @@ func TestResolveEndpoint(t *testing.T) { name: "with path parameter present", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { op := openapitest.NewOperation( - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent())), + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json")), openapitest.WithOperationParam("id", false)) - openapitest.AppendPath("/foo/{id}", c, openapitest.WithOperation("get", op)) + openapitest.AppendPath("/foo/{id}", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo/42", nil) rr := httptest.NewRecorder() h(rr, r) @@ -443,7 +442,7 @@ func TestResolveEndpoint(t *testing.T) { op := openapitest.NewOperation( openapitest.WithResponse(http.StatusOK), openapitest.WithOperationParam("id", false)) - openapitest.AppendPath("/foo/bar", c, openapitest.WithOperation("get", op)) + openapitest.AppendPath("/foo/bar", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo/bar", nil) rr := httptest.NewRecorder() h(rr, r) @@ -458,9 +457,9 @@ func TestResolveEndpoint(t *testing.T) { name: "with optional query parameter and not present", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { op := openapitest.NewOperation( - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent())), + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json")), openapitest.WithQueryParam("id", false)) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo", nil) rr := httptest.NewRecorder() h(rr, r) @@ -473,7 +472,7 @@ func TestResolveEndpoint(t *testing.T) { op := openapitest.NewOperation( openapitest.WithResponse(http.StatusOK), openapitest.WithQueryParam("id", true)) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo", nil) rr := httptest.NewRecorder() h(rr, r) @@ -485,9 +484,9 @@ func TestResolveEndpoint(t *testing.T) { name: "with required query parameter and present", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { op := openapitest.NewOperation( - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent())), + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json")), openapitest.WithQueryParam("id", true)) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo?id=42", nil) rr := httptest.NewRecorder() h(rr, r) @@ -501,9 +500,9 @@ func TestResolveEndpoint(t *testing.T) { name: "with optional query parameter and not present", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { op := openapitest.NewOperation( - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent())), + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json")), openapitest.WithCookieParam("id", false)) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo", nil) rr := httptest.NewRecorder() h(rr, r) @@ -516,7 +515,7 @@ func TestResolveEndpoint(t *testing.T) { op := openapitest.NewOperation( openapitest.WithResponse(http.StatusOK), openapitest.WithCookieParam("id", true)) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo", nil) rr := httptest.NewRecorder() h(rr, r) @@ -528,9 +527,9 @@ func TestResolveEndpoint(t *testing.T) { name: "with required query parameter and present", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { op := openapitest.NewOperation( - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent())), + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json")), openapitest.WithCookieParam("id", true)) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo", nil) r.AddCookie(&http.Cookie{Name: "id", Value: "42"}) rr := httptest.NewRecorder() @@ -545,9 +544,9 @@ func TestResolveEndpoint(t *testing.T) { name: "with optional query parameter and not present", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { op := openapitest.NewOperation( - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent())), + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json")), openapitest.WithHeaderParam("id", false)) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo", nil) rr := httptest.NewRecorder() h(rr, r) @@ -560,7 +559,7 @@ func TestResolveEndpoint(t *testing.T) { op := openapitest.NewOperation( openapitest.WithResponse(http.StatusOK), openapitest.WithHeaderParam("id", true)) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo", nil) rr := httptest.NewRecorder() h(rr, r) @@ -572,9 +571,9 @@ func TestResolveEndpoint(t *testing.T) { name: "with required query parameter and present", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { op := openapitest.NewOperation( - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent())), + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json")), openapitest.WithHeaderParam("id", true)) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo", nil) r.Header.Set("id", "42") rr := httptest.NewRecorder() @@ -589,9 +588,9 @@ func TestResolveEndpoint(t *testing.T) { name: "with content-type", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { op := openapitest.NewOperation( - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent())), + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json")), openapitest.WithHeaderParam("id", true)) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo", nil) r.Header.Set("id", "42") r.Header.Set("accept", "application/json") @@ -605,9 +604,9 @@ func TestResolveEndpoint(t *testing.T) { name: "with content-type extensions", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { op := openapitest.NewOperation( - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json;odata=verbose", openapitest.NewContent())), + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json;odata=verbose")), openapitest.WithHeaderParam("id", true)) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo", nil) r.Header.Set("id", "42") r.Header.Set("accept", "application/json;odata=verbose") @@ -621,9 +620,9 @@ func TestResolveEndpoint(t *testing.T) { name: "with content-type extensions", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { op := openapitest.NewOperation( - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent())), + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json")), openapitest.WithHeaderParam("id", true)) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo", nil) r.Header.Set("id", "42") r.Header.Set("accept", "application/json;odata=verbose") @@ -637,9 +636,9 @@ func TestResolveEndpoint(t *testing.T) { name: "with content-type extensions exactly", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { op := openapitest.NewOperation( - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json;odata=verbose", openapitest.NewContent())), + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json;odata=verbose")), openapitest.WithHeaderParam("id", true)) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo", nil) r.Header.Set("id", "42") r.Header.Set("accept", "application/json;odata=verbose") @@ -653,9 +652,9 @@ func TestResolveEndpoint(t *testing.T) { name: "with content-type multiple accepted", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { op := openapitest.NewOperation( - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent())), + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json")), openapitest.WithHeaderParam("id", true)) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo", nil) r.Header.Set("id", "42") r.Header.Set("accept", "text/plain,application/json") @@ -669,9 +668,9 @@ func TestResolveEndpoint(t *testing.T) { name: "with content-type not supported", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config) { op := openapitest.NewOperation( - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("text/plain", openapitest.NewContent())), + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("text/plain")), openapitest.WithHeaderParam("id", true)) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo", nil) r.Header.Set("id", "42") r.Header.Set("accept", "application/json") @@ -692,9 +691,9 @@ func TestResolveEndpoint(t *testing.T) { openapitest.WithOperationParam("petId", true, openapitest.WithParamSchema(schematest.New("integer")))) find := openapitest.NewOperation( openapitest.WithResponse(http.StatusOK), - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent()))) - openapitest.AppendPath("/pet/{petId}", c, openapitest.WithOperation("get", byId)) - openapitest.AppendPath("/pet/findByStatus", c, openapitest.WithOperation("get", find)) + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json"))) + openapitest.AppendPath("/pet/{petId}", c, openapitest.UseOperation("get", byId)) + openapitest.AppendPath("/pet/findByStatus", c, openapitest.UseOperation("get", find)) r := httptest.NewRequest("get", "http://localhost/pet/findByStatus", nil) r.Header.Set("accept", "application/json") rr := httptest.NewRecorder() @@ -711,21 +710,21 @@ func TestResolveEndpoint(t *testing.T) { byName := openapitest.NewOperation( openapitest.WithOperationParam("name", true, openapitest.WithParamSchema(schematest.New("string"))), openapitest.WithResponse(http.StatusOK, - openapitest.WithContent("application/json", openapitest.NewContent( + openapitest.WithContent("application/json", openapitest.WithSchema(schematest.New("string", schematest.WithConst("Name"))), - )), + ), ), ) find := openapitest.NewOperation( - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent())), + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json")), openapitest.WithResponse(http.StatusOK, - openapitest.WithContent("application/json", openapitest.NewContent( + openapitest.WithContent("application/json", openapitest.WithSchema(schematest.New("string", schematest.WithConst("findByStatus"))), - )), + ), ), ) - openapitest.AppendPath("/pet/{name}", c, openapitest.WithOperation("get", byName)) - openapitest.AppendPath("/pet/findByStatus", c, openapitest.WithOperation("get", find)) + openapitest.AppendPath("/pet/{name}", c, openapitest.UseOperation("get", byName)) + openapitest.AppendPath("/pet/findByStatus", c, openapitest.UseOperation("get", find)) r := httptest.NewRequest("get", "http://localhost/pet/findByStatus", nil) r.Header.Set("accept", "application/json") rr := httptest.NewRecorder() @@ -776,8 +775,8 @@ func TestHandler_Event(t *testing.T) { name: "no response found", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config, sm *events.StoreManager) { op := openapitest.NewOperation( - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent()))) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json"))) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo", nil) r.Header.Set("accept", "application/json") rr := httptest.NewRecorder() @@ -795,8 +794,8 @@ func TestHandler_Event(t *testing.T) { name: "event sets unknown status code", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config, sm *events.StoreManager) { op := openapitest.NewOperation( - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent()))) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json"))) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo", nil) r.Header.Set("accept", "application/json") rr := httptest.NewRecorder() @@ -815,9 +814,9 @@ func TestHandler_Event(t *testing.T) { test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config, sm *events.StoreManager) { op := openapitest.NewOperation( openapitest.WithResponse(http.StatusOK, - openapitest.WithContent("application/json", openapitest.NewContent()), - openapitest.WithContent("text/plain", openapitest.NewContent()))) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + openapitest.WithContent("application/json"), + openapitest.WithContent("text/plain"))) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo", nil) r.Header.Set("accept", "application/json") rr := httptest.NewRecorder() @@ -839,9 +838,9 @@ func TestHandler_Event(t *testing.T) { openapitest.WithRequestBody("", true, openapitest.WithRequestContent("application/json", openapitest.NewContent())), openapitest.WithResponse(http.StatusOK, - openapitest.WithContent("application/json", openapitest.NewContent()), + openapitest.WithContent("application/json"), )) - openapitest.AppendPath("/foo", c, openapitest.WithOperation(http.MethodPost, op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation(http.MethodPost, op)) r := httptest.NewRequest("post", "http://localhost/foo", strings.NewReader(`{ "foo": "bar" }`)) r.Header.Set("Content-Type", "application/json") rr := httptest.NewRecorder() @@ -861,9 +860,9 @@ func TestHandler_Event(t *testing.T) { test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config, sm *events.StoreManager) { op := openapitest.NewOperation( openapitest.WithResponse(http.StatusOK, - openapitest.WithContent("application/json", openapitest.NewContent()), + openapitest.WithContent("application/json"), )) - openapitest.AppendPath("/foo", c, openapitest.WithOperation(http.MethodPost, op)) + openapitest.AppendPath("/foo", c, openapitest.UseOperation(http.MethodPost, op)) r := httptest.NewRequest("post", "http://localhost/foo", strings.NewReader(`{ "foo": "bar" }`)) r.Header.Set("Content-Type", "application/json") rr := httptest.NewRecorder() @@ -883,10 +882,10 @@ func TestHandler_Event(t *testing.T) { test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config, sm *events.StoreManager) { op := openapitest.NewOperation( openapitest.WithResponse(http.StatusOK, - openapitest.WithContent("application/json", openapitest.NewContent()), + openapitest.WithContent("application/json"), )) openapitest.AppendPath("/foo/{id}", c, - openapitest.WithOperation(http.MethodPost, op), + openapitest.UseOperation(http.MethodPost, op), openapitest.WithPathParam("id", openapitest.WithParamSchema(schematest.New("string"))), ) r := httptest.NewRequest("post", "http://localhost/foo/123", strings.NewReader(`{ "foo": "bar" }`)) @@ -908,10 +907,10 @@ func TestHandler_Event(t *testing.T) { test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config, sm *events.StoreManager) { op := openapitest.NewOperation( openapitest.WithResponse(http.StatusOK, - openapitest.WithContent("application/json", openapitest.NewContent()), + openapitest.WithContent("application/json"), )) openapitest.AppendPath("/foo/{id}/", c, - openapitest.WithOperation(http.MethodPost, op), + openapitest.UseOperation(http.MethodPost, op), openapitest.WithPathParam("id", openapitest.WithParamSchema(schematest.New("string"))), ) r := httptest.NewRequest("post", "http://localhost/foo/123", strings.NewReader(`{ "foo": "bar" }`)) @@ -933,10 +932,10 @@ func TestHandler_Event(t *testing.T) { test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config, sm *events.StoreManager) { op := openapitest.NewOperation( openapitest.WithResponse(http.StatusOK, - openapitest.WithContent("application/json", openapitest.NewContent()), + openapitest.WithContent("application/json"), )) openapitest.AppendPath("/foo/{id}/", c, - openapitest.WithOperation(http.MethodPost, op), + openapitest.UseOperation(http.MethodPost, op), openapitest.WithPathParam("id", openapitest.WithParamSchema(schematest.New("string"))), ) r := httptest.NewRequest("post", "http://localhost/foo/123", strings.NewReader(`{ "foo": "bar" }`)) @@ -957,10 +956,10 @@ func TestHandler_Event(t *testing.T) { test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config, sm *events.StoreManager) { op := openapitest.NewOperation( openapitest.WithResponse(http.StatusOK, - openapitest.WithContent("application/json", openapitest.NewContent()), + openapitest.WithContent("application/json"), )) openapitest.AppendPath("/foo/{id}/", c, - openapitest.WithOperation(http.MethodPost, op), + openapitest.UseOperation(http.MethodPost, op), openapitest.WithPathParam("id", openapitest.WithParamSchema(schematest.New("string"))), ) r := httptest.NewRequest("post", "http://localhost/foo/123", strings.NewReader(`{ "foo": "bar" }`)) @@ -987,14 +986,14 @@ func TestHandler_Event(t *testing.T) { test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config, sm *events.StoreManager) { op := openapitest.NewOperation( openapitest.WithResponse(http.StatusOK, - openapitest.WithContent("application/json", openapitest.NewContent()), + openapitest.WithContent("application/json"), ), openapitest.WithOperationParam("id", true, openapitest.WithParamSchema(schematest.New("integer"))), openapitest.WithOperationId("foo-operation"), ) openapitest.AppendPath("/foo/{id}", c, - openapitest.WithOperation(http.MethodGet, op), + openapitest.UseOperation(http.MethodGet, op), ) r := httptest.NewRequest(http.MethodGet, "http://localhost/foo/123", nil) rr := httptest.NewRecorder() @@ -1035,12 +1034,12 @@ func TestHandler_Event(t *testing.T) { test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config, sm *events.StoreManager) { op := openapitest.NewOperation( openapitest.WithResponse(http.StatusOK, - openapitest.WithContent("application/json", openapitest.NewContent()), - openapitest.WithContent("text/plain", openapitest.NewContent()), + openapitest.WithContent("application/json"), + openapitest.WithContent("text/plain"), ), ) openapitest.AppendPath("/foo", c, - openapitest.WithOperation(http.MethodGet, op), + openapitest.UseOperation(http.MethodGet, op), ) r := httptest.NewRequest(http.MethodGet, "http://localhost/foo", nil) rr := httptest.NewRecorder() @@ -1059,11 +1058,11 @@ func TestHandler_Event(t *testing.T) { test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config, sm *events.StoreManager) { op := openapitest.NewOperation( openapitest.WithResponse(http.StatusOK, - openapitest.WithContent("application/json", openapitest.NewContent()), + openapitest.WithContent("application/json"), ), ) openapitest.AppendPath("/foo", c, - openapitest.WithOperation(http.MethodGet, op), + openapitest.UseOperation(http.MethodGet, op), ) r := httptest.NewRequest(http.MethodGet, "http://localhost/foo", nil) rr := httptest.NewRecorder() @@ -1082,11 +1081,11 @@ func TestHandler_Event(t *testing.T) { test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config, sm *events.StoreManager) { op := openapitest.NewOperation( openapitest.WithResponse(http.StatusOK, - openapitest.WithContent("application/json", openapitest.NewContent()), + openapitest.WithContent("application/json"), ), ) openapitest.AppendPath("/foo", c, - openapitest.WithOperation(http.MethodGet, op), + openapitest.UseOperation(http.MethodGet, op), ) r := httptest.NewRequest(http.MethodGet, "http://localhost/foo", nil) rr := httptest.NewRecorder() @@ -1105,12 +1104,12 @@ func TestHandler_Event(t *testing.T) { test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config, sm *events.StoreManager) { op := openapitest.NewOperation( openapitest.WithResponse(http.StatusOK, - openapitest.WithContent("application/json", openapitest.NewContent()), + openapitest.WithContent("application/json"), openapitest.WithResponseHeader("foo", "", schematest.New("array")), ), ) openapitest.AppendPath("/foo", c, - openapitest.WithOperation(http.MethodGet, op), + openapitest.UseOperation(http.MethodGet, op), ) r := httptest.NewRequest(http.MethodGet, "http://localhost/foo", nil) rr := httptest.NewRecorder() @@ -1129,12 +1128,12 @@ func TestHandler_Event(t *testing.T) { test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config, sm *events.StoreManager) { op := openapitest.NewOperation( openapitest.WithResponse(http.StatusOK, - openapitest.WithContent("application/json", openapitest.NewContent()), + openapitest.WithContent("application/json"), openapitest.WithResponseHeader("foo", "", schematest.New("array", schematest.WithItems("integer"))), ), ) openapitest.AppendPath("/foo", c, - openapitest.WithOperation(http.MethodGet, op), + openapitest.UseOperation(http.MethodGet, op), ) r := httptest.NewRequest(http.MethodGet, "http://localhost/foo", nil) rr := httptest.NewRecorder() @@ -1193,8 +1192,8 @@ func TestHandler_Log(t *testing.T) { name: "simple", test: func(t *testing.T, h http.HandlerFunc, c *openapi.Config, eh events.Handler) { op := openapitest.NewOperation( - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent()))) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", op)) + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json"))) + openapitest.AppendPath("/foo", c, openapitest.UseOperation("get", op)) r := httptest.NewRequest("GET", "http://localhost/foo", nil) r.Header.Set("accept", "application/json") rr := httptest.NewRecorder() @@ -1282,8 +1281,8 @@ func TestHandler_Event_TypeScript(t *testing.T) { } op := openapitest.NewOperation( - openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", openapitest.NewContent()))) - openapitest.AppendPath("/foo", config, openapitest.WithOperation("get", op)) + openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json"))) + openapitest.AppendPath("/foo", config, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo", nil) r.Header.Set("accept", "application/json") rr := httptest.NewRecorder() @@ -1321,25 +1320,25 @@ func TestHandler_Event_TypeScript(t *testing.T) { op := openapitest.NewOperation( openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", - openapitest.NewContent(openapitest.WithSchema( + openapitest.WithSchema( schematest.New( "object", schematest.WithProperty("foo", schematest.New("string")), schematest.WithRequired("foo"), ), - )), + ), )), openapitest.WithResponse(http.StatusNotFound, openapitest.WithContent("application/json", - openapitest.NewContent(openapitest.WithSchema( + openapitest.WithSchema( schematest.New( "object", schematest.WithProperty("message", schematest.New("string")), schematest.WithRequired("message"), ), - )), + ), )), ) - openapitest.AppendPath("/foo", config, openapitest.WithOperation("get", op)) + openapitest.AppendPath("/foo", config, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo", nil) r.Header.Set("accept", "application/json") rr := httptest.NewRecorder() @@ -1377,16 +1376,16 @@ func TestHandler_Event_TypeScript(t *testing.T) { op := openapitest.NewOperation( openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", - openapitest.NewContent(openapitest.WithSchema( + openapitest.WithSchema( schematest.New( "object", schematest.WithProperty("foo", schematest.New("string")), schematest.WithRequired("foo"), ), - )), + ), )), ) - openapitest.AppendPath("/foo", config, openapitest.WithOperation("get", op)) + openapitest.AppendPath("/foo", config, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo", nil) r.Header.Set("accept", "application/json") rr := httptest.NewRecorder() @@ -1429,26 +1428,26 @@ func TestHandler_Event_TypeScript(t *testing.T) { op := openapitest.NewOperation( openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", - openapitest.NewContent(openapitest.WithSchema( + openapitest.WithSchema( schematest.New( "object", schematest.WithProperty("foo", schematest.New("string")), schematest.WithRequired("foo"), ), - )), + ), ), openapitest.WithContent("application/xml", - openapitest.NewContent(openapitest.WithSchema( + openapitest.WithSchema( schematest.New( "object", schematest.WithProperty("foo", schematest.New("string")), schematest.WithRequired("foo"), ), - )), + ), ), ), ) - openapitest.AppendPath("/foo", config, openapitest.WithOperation("get", op)) + openapitest.AppendPath("/foo", config, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo", nil) r.Header.Set("accept", "application/json") rr := httptest.NewRecorder() @@ -1486,25 +1485,25 @@ func TestHandler_Event_TypeScript(t *testing.T) { op := openapitest.NewOperation( openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", - openapitest.NewContent(openapitest.WithSchema( + openapitest.WithSchema( schematest.New( "object", schematest.WithProperty("foo", schematest.New("string")), schematest.WithRequired("foo"), ), - )), + ), )), openapitest.WithResponse(http.StatusNotFound, openapitest.WithContent("application/json", - openapitest.NewContent(openapitest.WithSchema( + openapitest.WithSchema( schematest.New( "object", schematest.WithProperty("message", schematest.New("string")), schematest.WithRequired("message"), ), - )), + ), )), ) - openapitest.AppendPath("/foo", config, openapitest.WithOperation("get", op)) + openapitest.AppendPath("/foo", config, openapitest.UseOperation("get", op)) r := httptest.NewRequest("get", "http://localhost/foo", nil) r.Header.Set("accept", "application/json") rr := httptest.NewRecorder() @@ -1540,10 +1539,10 @@ func TestHandler_Parameter(t *testing.T) { op := openapitest.NewOperation( openapitest.WithResponse(http.StatusOK, - openapitest.WithContent("application/json", openapitest.NewContent()), + openapitest.WithContent("application/json"), )) openapitest.AppendPath("/foo/{id}", c, - openapitest.WithOperation(http.MethodPost, op), + openapitest.UseOperation(http.MethodPost, op), ) r := httptest.NewRequest("post", "http://localhost/foo/123", strings.NewReader(`{ "foo": "bar" }`)) r.Header.Set("Content-Type", "application/json") diff --git a/providers/openapi/header_test.go b/providers/openapi/header_test.go index 9f813229a..e6b15162c 100644 --- a/providers/openapi/header_test.go +++ b/providers/openapi/header_test.go @@ -3,8 +3,6 @@ package openapi_test import ( "encoding/json" "fmt" - "github.com/stretchr/testify/require" - "gopkg.in/yaml.v3" "mokapi/config/dynamic" "mokapi/config/dynamic/dynamictest" "mokapi/providers/openapi" @@ -13,6 +11,9 @@ import ( "net/http" "net/url" "testing" + + "github.com/stretchr/testify/require" + "gopkg.in/yaml.v3" ) func TestHeader_UnmarshalJSON(t *testing.T) { @@ -241,12 +242,12 @@ func TestHeader_Parse(t *testing.T) { return nil, nil }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation(http.MethodGet, openapitest.NewOperation( + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodGet, openapitest.WithResponse(http.StatusOK, - openapitest.WithResponseHeaderRef("foo", nil), - ))), - )), + openapitest.UseResponseHeaderRef("foo", nil), + )), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.NoError(t, err) @@ -259,12 +260,12 @@ func TestHeader_Parse(t *testing.T) { return nil, nil }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation(http.MethodGet, openapitest.NewOperation( + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodGet, openapitest.WithResponse(http.StatusOK, openapitest.WithResponseHeader("foo", "foo description", schematest.New("string")), - ))), - )), + )), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.NoError(t, err) @@ -279,12 +280,12 @@ func TestHeader_Parse(t *testing.T) { return nil, fmt.Errorf("TEST ERROR") }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation(http.MethodGet, openapitest.NewOperation( + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodGet, openapitest.WithResponse(http.StatusOK, - openapitest.WithResponseHeaderRef("foo", &openapi.HeaderRef{Reference: dynamic.Reference{Ref: "foo"}}), - ))), - )), + openapitest.WithResponseHeaderRef("foo", "foo"), + )), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.EqualError(t, err, "parse path '/foo' failed: parse operation 'GET' failed: parse response '200' failed: parse header 'foo' failed: resolve reference 'foo' failed: TEST ERROR") @@ -312,25 +313,25 @@ func TestConfig_Patch_Header(t *testing.T) { name: "add header", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponseRef(200, &openapi.ResponseRef{Value: &openapi.Response{ - Headers: map[string]*openapi.HeaderRef{}, - }}), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.UseResponse(200, &openapi.Response{ + Headers: map[string]*openapi.HeaderRef{}, + }), ), - ))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponseRef(200, &openapi.ResponseRef{Value: &openapi.Response{ - Headers: map[string]*openapi.HeaderRef{ - "foo": {Value: &openapi.Header{Parameter: openapi.Parameter{Description: "foo"}}}, - }, - }}), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.UseResponse(200, &openapi.Response{ + Headers: map[string]*openapi.HeaderRef{ + "foo": {Value: &openapi.Header{Parameter: openapi.Parameter{Description: "foo"}}}, + }, + }), ), - ))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) @@ -341,27 +342,27 @@ func TestConfig_Patch_Header(t *testing.T) { name: "patch header", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponseRef(200, &openapi.ResponseRef{Value: &openapi.Response{ - Headers: map[string]*openapi.HeaderRef{ - "foo": {Value: &openapi.Header{Parameter: openapi.Parameter{Description: "foo"}}}, - }, - }}), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.UseResponse(200, &openapi.Response{ + Headers: map[string]*openapi.HeaderRef{ + "foo": {Value: &openapi.Header{Parameter: openapi.Parameter{Description: "foo"}}}, + }, + }), ), - ))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponseRef(200, &openapi.ResponseRef{Value: &openapi.Response{ - Headers: map[string]*openapi.HeaderRef{ - "foo": {Value: &openapi.Header{Parameter: openapi.Parameter{Description: "bar"}}}, - }, - }}), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.UseResponse(200, &openapi.Response{ + Headers: map[string]*openapi.HeaderRef{ + "foo": {Value: &openapi.Header{Parameter: openapi.Parameter{Description: "bar"}}}, + }, + }), ), - ))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) @@ -372,27 +373,27 @@ func TestConfig_Patch_Header(t *testing.T) { name: "source header is nil", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponseRef(200, &openapi.ResponseRef{Value: &openapi.Response{ - Headers: map[string]*openapi.HeaderRef{ - "foo": {Value: nil}, - }, - }}), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.UseResponse(200, &openapi.Response{ + Headers: map[string]*openapi.HeaderRef{ + "foo": {Value: nil}, + }, + }), ), - ))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponseRef(200, &openapi.ResponseRef{Value: &openapi.Response{ - Headers: map[string]*openapi.HeaderRef{ - "foo": {Value: &openapi.Header{Parameter: openapi.Parameter{Description: "foo"}}}, - }, - }}), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.UseResponse(200, &openapi.Response{ + Headers: map[string]*openapi.HeaderRef{ + "foo": {Value: &openapi.Header{Parameter: openapi.Parameter{Description: "foo"}}}, + }, + }), ), - ))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) @@ -403,27 +404,27 @@ func TestConfig_Patch_Header(t *testing.T) { name: "source header value is nil", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponseRef(200, &openapi.ResponseRef{Value: &openapi.Response{ - Headers: map[string]*openapi.HeaderRef{ - "foo": {Value: nil}, - }, - }}), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.UseResponse(200, &openapi.Response{ + Headers: map[string]*openapi.HeaderRef{ + "foo": {Value: nil}, + }, + }), ), - ))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponseRef(200, &openapi.ResponseRef{Value: &openapi.Response{ - Headers: map[string]*openapi.HeaderRef{ - "foo": {Value: &openapi.Header{Parameter: openapi.Parameter{Description: "foo"}}}, - }, - }}), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.UseResponse(200, &openapi.Response{ + Headers: map[string]*openapi.HeaderRef{ + "foo": {Value: &openapi.Header{Parameter: openapi.Parameter{Description: "foo"}}}, + }, + }), ), - ))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) @@ -434,27 +435,27 @@ func TestConfig_Patch_Header(t *testing.T) { name: "patch headers is nil", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponseRef(200, &openapi.ResponseRef{Value: &openapi.Response{ - Headers: map[string]*openapi.HeaderRef{ - "foo": {Value: &openapi.Header{Parameter: openapi.Parameter{Description: "foo"}}}, - }, - }}), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.UseResponse(200, &openapi.Response{ + Headers: map[string]*openapi.HeaderRef{ + "foo": {Value: &openapi.Header{Parameter: openapi.Parameter{Description: "foo"}}}, + }, + }), ), - ))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponseRef(200, &openapi.ResponseRef{Value: &openapi.Response{ - Headers: map[string]*openapi.HeaderRef{ - "foo": nil, - }, - }}), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.UseResponse(200, &openapi.Response{ + Headers: map[string]*openapi.HeaderRef{ + "foo": nil, + }, + }), ), - ))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) @@ -465,27 +466,27 @@ func TestConfig_Patch_Header(t *testing.T) { name: "patch header value is nil", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponseRef(200, &openapi.ResponseRef{Value: &openapi.Response{ - Headers: map[string]*openapi.HeaderRef{ - "foo": {Value: &openapi.Header{Parameter: openapi.Parameter{Description: "foo"}}}, - }, - }}), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.UseResponse(200, &openapi.Response{ + Headers: map[string]*openapi.HeaderRef{ + "foo": {Value: &openapi.Header{Parameter: openapi.Parameter{Description: "foo"}}}, + }, + }), ), - ))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponseRef(200, &openapi.ResponseRef{Value: &openapi.Response{ - Headers: map[string]*openapi.HeaderRef{ - "foo": {}, - }, - }}), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.UseResponse(200, &openapi.Response{ + Headers: map[string]*openapi.HeaderRef{ + "foo": {}, + }, + }), ), - ))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) diff --git a/providers/openapi/media_type_test.go b/providers/openapi/media_type_test.go index 3e1102159..c86974cec 100644 --- a/providers/openapi/media_type_test.go +++ b/providers/openapi/media_type_test.go @@ -3,8 +3,6 @@ package openapi_test import ( "encoding/json" "fmt" - "github.com/stretchr/testify/require" - "gopkg.in/yaml.v3" "mokapi/config/dynamic" "mokapi/config/dynamic/dynamictest" "mokapi/providers/openapi" @@ -13,6 +11,9 @@ import ( "net/http" "net/url" "testing" + + "github.com/stretchr/testify/require" + "gopkg.in/yaml.v3" ) func TestMediaType_UnmarshalJSON(t *testing.T) { @@ -109,12 +110,12 @@ func TestMediaType_Parse(t *testing.T) { return nil, nil }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation(http.MethodGet, openapitest.NewOperation( + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodGet, openapitest.WithResponse(http.StatusOK, - openapitest.WithContent("application/json", &openapi.MediaType{}), - ))), - )), + openapitest.WithContent("application/json"), + )), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.NoError(t, err) @@ -127,12 +128,12 @@ func TestMediaType_Parse(t *testing.T) { return nil, nil }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation(http.MethodGet, openapitest.NewOperation( + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodGet, openapitest.WithResponse(http.StatusOK, - openapitest.WithContent("application/json", nil), - ))), - )), + openapitest.UseContent("application/json", nil), + )), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.NoError(t, err) @@ -145,18 +146,16 @@ func TestMediaType_Parse(t *testing.T) { return nil, fmt.Errorf("TEST ERROR") }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation(http.MethodGet, openapitest.NewOperation( + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodGet, openapitest.WithResponse(http.StatusOK, openapitest.WithContent("application/json", - openapitest.NewContent( - openapitest.WithSchemaRef("foo.yml"), - ), + + openapitest.WithSchemaRef("foo.yml"), ), ), ), - ), - )), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.EqualError(t, err, "parse path '/foo' failed: parse operation 'GET' failed: parse response '200' failed: parse content 'application/json' failed: parse schema failed: resolve reference 'foo.yml' failed: TEST ERROR") @@ -169,16 +168,15 @@ func TestMediaType_Parse(t *testing.T) { return nil, fmt.Errorf("TEST ERROR") }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation(http.MethodGet, openapitest.NewOperation( + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodGet, openapitest.WithResponse(http.StatusOK, - openapitest.WithContent("application/json", + openapitest.UseContent("application/json", &openapi.MediaType{Examples: map[string]*openapi.ExampleRef{"foo": {Reference: dynamic.Reference{Ref: "foo.yml"}}}}, ), ), ), - ), - )), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.EqualError(t, err, "parse path '/foo' failed: parse operation 'GET' failed: parse response '200' failed: parse content 'application/json' failed: parse example 'foo' failed: resolve reference 'foo.yml' failed: TEST ERROR") @@ -206,19 +204,19 @@ func TestConfig_Patch_MediaType(t *testing.T) { name: "patch is nil", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/plain", &openapi.MediaType{})), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.WithContent("text/plain")), ), - ))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/html", nil)), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.UseContent("text/html", nil)), ), - ))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) @@ -231,19 +229,19 @@ func TestConfig_Patch_MediaType(t *testing.T) { name: "add schema", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/html", &openapi.MediaType{})), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.WithContent("text/html")), ), - ))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/html", &openapi.MediaType{Schema: schematest.New("string")})), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.UseContent("text/html", &openapi.MediaType{Schema: schematest.New("string")})), ), - ))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) @@ -255,19 +253,19 @@ func TestConfig_Patch_MediaType(t *testing.T) { name: "patch schema", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/html", &openapi.MediaType{Schema: schematest.New("string")})), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.UseContent("text/html", &openapi.MediaType{Schema: schematest.New("string")})), ), - ))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/html", &openapi.MediaType{Schema: schematest.New("object")})), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.UseContent("text/html", &openapi.MediaType{Schema: schematest.New("object")})), ), - ))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) @@ -279,19 +277,19 @@ func TestConfig_Patch_MediaType(t *testing.T) { name: "add example", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/html", &openapi.MediaType{})), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.WithContent("text/html")), ), - ))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/html", &openapi.MediaType{Example: &openapi.ExampleValue{Value: "foo"}})), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.UseContent("text/html", &openapi.MediaType{Example: &openapi.ExampleValue{Value: "foo"}})), ), - ))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) @@ -303,19 +301,19 @@ func TestConfig_Patch_MediaType(t *testing.T) { name: "patch example", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/html", &openapi.MediaType{Example: &openapi.ExampleValue{Value: "foo"}})), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.UseContent("text/html", &openapi.MediaType{Example: &openapi.ExampleValue{Value: "foo"}})), ), - ))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/html", &openapi.MediaType{Example: &openapi.ExampleValue{Value: "bar"}})), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.UseContent("text/html", &openapi.MediaType{Example: &openapi.ExampleValue{Value: "bar"}})), ), - ))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) @@ -327,19 +325,19 @@ func TestConfig_Patch_MediaType(t *testing.T) { name: "patch example is nil", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/html", &openapi.MediaType{Example: &openapi.ExampleValue{Value: "foo"}})), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.UseContent("text/html", &openapi.MediaType{Example: &openapi.ExampleValue{Value: "foo"}})), ), - ))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/html", &openapi.MediaType{Example: nil})), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.UseContent("text/html", &openapi.MediaType{Example: nil})), ), - ))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) @@ -351,21 +349,21 @@ func TestConfig_Patch_MediaType(t *testing.T) { name: "add examples", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/html", &openapi.MediaType{})), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.WithContent("text/html")), ), - ))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/html", &openapi.MediaType{ - Examples: map[string]*openapi.ExampleRef{"foo": {Value: &openapi.Example{Value: "foo"}}}, - })), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.UseContent("text/html", &openapi.MediaType{ + Examples: map[string]*openapi.ExampleRef{"foo": {Value: &openapi.Example{Value: "foo"}}}, + })), ), - ))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) @@ -377,23 +375,23 @@ func TestConfig_Patch_MediaType(t *testing.T) { name: "patch examples foo", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/html", &openapi.MediaType{ - Examples: map[string]*openapi.ExampleRef{"foo": {Value: &openapi.Example{Value: "foo"}}}, - })), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.UseContent("text/html", &openapi.MediaType{ + Examples: map[string]*openapi.ExampleRef{"foo": {Value: &openapi.Example{Value: "foo"}}}, + })), ), - ))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/html", &openapi.MediaType{ - Examples: map[string]*openapi.ExampleRef{"foo": {Value: &openapi.Example{Value: "bar"}}}, - })), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.UseContent("text/html", &openapi.MediaType{ + Examples: map[string]*openapi.ExampleRef{"foo": {Value: &openapi.Example{Value: "bar"}}}, + })), ), - ))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) @@ -405,21 +403,21 @@ func TestConfig_Patch_MediaType(t *testing.T) { name: "patch example is nil", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/html", &openapi.MediaType{ - Examples: map[string]*openapi.ExampleRef{"foo": {Value: &openapi.Example{Value: "foo"}}}, - })), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.UseContent("text/html", &openapi.MediaType{ + Examples: map[string]*openapi.ExampleRef{"foo": {Value: &openapi.Example{Value: "foo"}}}, + })), ), - ))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/html", &openapi.MediaType{Examples: nil})), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.UseContent("text/html", &openapi.MediaType{Examples: nil})), ), - ))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) @@ -431,23 +429,23 @@ func TestConfig_Patch_MediaType(t *testing.T) { name: "patch examples overwrites example", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/html", &openapi.MediaType{ - Example: &openapi.ExampleValue{Value: "foo"}, - })), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.UseContent("text/html", &openapi.MediaType{ + Example: &openapi.ExampleValue{Value: "foo"}, + })), ), - ))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/html", &openapi.MediaType{ - Examples: map[string]*openapi.ExampleRef{"foo": {Value: &openapi.Example{Value: "bar"}}}, - })), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.UseContent("text/html", &openapi.MediaType{ + Examples: map[string]*openapi.ExampleRef{"foo": {Value: &openapi.Example{Value: "bar"}}}, + })), ), - ))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) @@ -460,23 +458,23 @@ func TestConfig_Patch_MediaType(t *testing.T) { name: "patch example overwrites examples", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/html", &openapi.MediaType{ - Examples: map[string]*openapi.ExampleRef{"foo": {Value: &openapi.Example{Value: "foo"}}}, - })), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.UseContent("text/html", &openapi.MediaType{ + Examples: map[string]*openapi.ExampleRef{"foo": {Value: &openapi.Example{Value: "foo"}}}, + })), ), - ))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/html", &openapi.MediaType{ - Example: &openapi.ExampleValue{Value: "bar"}, - })), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.UseContent("text/html", &openapi.MediaType{ + Example: &openapi.ExampleValue{Value: "bar"}, + })), ), - ))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) diff --git a/providers/openapi/openapitest/config.go b/providers/openapi/openapitest/config.go index b1db30fb0..7e6bd5493 100644 --- a/providers/openapi/openapitest/config.go +++ b/providers/openapi/openapitest/config.go @@ -56,8 +56,9 @@ func WithTag(name, summary, description string) ConfigOptions { } } -func WithPath(name string, path *openapi.Path) ConfigOptions { +func WithPath(name string, opts ...PathOptions) ConfigOptions { return func(c *openapi.Config) { + path := NewPath(opts...) if path != nil { path.Path = name } @@ -65,6 +66,15 @@ func WithPath(name string, path *openapi.Path) ConfigOptions { } } +func UsePath(name string, p *openapi.Path) ConfigOptions { + return func(c *openapi.Config) { + if p != nil { + p.Path = name + } + c.Paths[name] = &openapi.PathRef{Value: p} + } +} + func WithPathRef(name string, ref *openapi.PathRef) ConfigOptions { return func(c *openapi.Config) { if ref != nil && ref.Value != nil { diff --git a/providers/openapi/openapitest/operation.go b/providers/openapi/openapitest/operation.go index 5a370395b..8defe7640 100644 --- a/providers/openapi/openapitest/operation.go +++ b/providers/openapi/openapitest/operation.go @@ -1,6 +1,7 @@ package openapitest import ( + "mokapi/config/dynamic" "mokapi/media" "mokapi/providers/openapi" "mokapi/providers/openapi/schema" @@ -36,7 +37,21 @@ func WithResponse(status int, opts ...ResponseOptions) OperationOptions { } } -func WithResponseRef(status int, ref *openapi.ResponseRef) OperationOptions { +func UseResponse(status int, r *openapi.Response) OperationOptions { + return func(o *openapi.Operation) { + o.Responses.Set(strconv.Itoa(status), &openapi.ResponseRef{Value: r}) + } +} + +func WithResponseRef(status int, ref string) OperationOptions { + return func(o *openapi.Operation) { + o.Responses.Set(strconv.Itoa(status), &openapi.ResponseRef{ + Reference: dynamic.Reference{Ref: ref}, + }) + } +} + +func UseResponseRef(status int, ref *openapi.ResponseRef) OperationOptions { return func(o *openapi.Operation) { o.Responses.Set(strconv.Itoa(status), ref) } @@ -52,10 +67,10 @@ func WithOperationParam(name string, required bool, opts ...ParamOptions) Operat } } -func WithOperationParamRef(ref *openapi.ParameterRef) OperationOptions { +func WithOperationParamRef(ref string) OperationOptions { return func(o *openapi.Operation) { - o.Parameters = append(o.Parameters, ref) + o.Parameters = append(o.Parameters, &openapi.ParameterRef{Reference: dynamic.Reference{Ref: ref}}) } } @@ -110,6 +125,14 @@ func WithRequestBody(description string, required bool, opts ...RequestBodyOptio } } +func WithRequestBodyRef(ref string) OperationOptions { + return func(o *openapi.Operation) { + o.RequestBody = &openapi.RequestBodyRef{ + Reference: dynamic.Reference{Ref: ref}, + } + } +} + func newParam(name string, required bool, t openapi.Location, opts ...ParamOptions) *openapi.Parameter { p := &openapi.Parameter{ Name: name, diff --git a/providers/openapi/openapitest/path.go b/providers/openapi/openapitest/path.go index 88b52aa1f..7eade336b 100644 --- a/providers/openapi/openapitest/path.go +++ b/providers/openapi/openapitest/path.go @@ -1,6 +1,7 @@ package openapitest import ( + "mokapi/config/dynamic" "mokapi/providers/openapi" "strings" ) @@ -36,7 +37,7 @@ func AppendPath(path string, config *openapi.Config, opts ...PathOptions) *opena return e } -func WithOperation(method string, op *openapi.Operation) PathOptions { +func UseOperation(method string, op *openapi.Operation) PathOptions { return func(e *openapi.Path) { switch strings.ToUpper(method) { case "GET": @@ -67,6 +68,11 @@ func WithOperation(method string, op *openapi.Operation) PathOptions { } } +func WithOperation(method string, opts ...OperationOptions) PathOptions { + op := NewOperation(opts...) + return UseOperation(method, op) +} + func WithPathParam(name string, opts ...ParamOptions) PathOptions { return func(e *openapi.Path) { e.Parameters = append(e.Parameters, &openapi.ParameterRef{ @@ -74,8 +80,10 @@ func WithPathParam(name string, opts ...ParamOptions) PathOptions { } } -func WithPathParamRef(ref *openapi.ParameterRef) PathOptions { +func WithPathParamRef(ref string) PathOptions { return func(e *openapi.Path) { - e.Parameters = append(e.Parameters, ref) + e.Parameters = append(e.Parameters, &openapi.ParameterRef{ + Reference: dynamic.Reference{Ref: ref}, + }) } } diff --git a/providers/openapi/openapitest/response.go b/providers/openapi/openapitest/response.go index 2663fedf4..8781a5be8 100644 --- a/providers/openapi/openapitest/response.go +++ b/providers/openapi/openapitest/response.go @@ -1,6 +1,7 @@ package openapitest import ( + "mokapi/config/dynamic" "mokapi/media" "mokapi/providers/openapi" "mokapi/providers/openapi/schema" @@ -35,7 +36,16 @@ func WithResponseHeader(name, description string, s *schema.Schema) ResponseOpti } } -func WithResponseHeaderRef(name string, ref *openapi.HeaderRef) ResponseOptions { +func WithResponseHeaderRef(name string, ref string) ResponseOptions { + return func(o *openapi.Response) { + if o.Headers == nil { + o.Headers = map[string]*openapi.HeaderRef{} + } + o.Headers[name] = &openapi.HeaderRef{Reference: dynamic.Reference{Ref: ref}} + } +} + +func UseResponseHeaderRef(name string, ref *openapi.HeaderRef) ResponseOptions { return func(o *openapi.Response) { if o.Headers == nil { o.Headers = map[string]*openapi.HeaderRef{} @@ -50,9 +60,10 @@ func WithResponseDescription(description string) ResponseOptions { } } -func WithContent(mediaType string, content *openapi.MediaType) ResponseOptions { +func WithContent(mediaType string, opts ...ContentOptions) ResponseOptions { return func(o *openapi.Response) { ct := media.ParseContentType(mediaType) + content := NewContent(opts...) o.Content[mediaType] = content if content != nil { content.ContentType = ct @@ -60,6 +71,18 @@ func WithContent(mediaType string, content *openapi.MediaType) ResponseOptions { } } +func UseContent(mediaType string, mt *openapi.MediaType) ResponseOptions { + return func(o *openapi.Response) { + if o.Content == nil { + o.Content = map[string]*openapi.MediaType{} + } + o.Content[mediaType] = mt + if mt != nil { + mt.ContentType = media.ParseContentType(mediaType) + } + } +} + func NewContent(opts ...ContentOptions) *openapi.MediaType { mt := &openapi.MediaType{} for _, opt := range opts { @@ -78,12 +101,21 @@ func WithEncoding(propName string, encoding *openapi.Encoding) ContentOptions { } } -func WithExample(example interface{}) ContentOptions { +func WithExampleValue(example interface{}) ContentOptions { return func(c *openapi.MediaType) { c.Example = &openapi.ExampleValue{Value: example} } } +func WithExampleRef(name, ref string) ContentOptions { + return func(c *openapi.MediaType) { + if c.Examples == nil { + c.Examples = map[string]*openapi.ExampleRef{} + } + c.Examples[name] = &openapi.ExampleRef{Reference: dynamic.Reference{Ref: ref}} + } +} + func WithSchema(s *schema.Schema) ContentOptions { return func(c *openapi.MediaType) { c.Schema = s diff --git a/providers/openapi/operation_test.go b/providers/openapi/operation_test.go index efd734146..e4eb51271 100644 --- a/providers/openapi/operation_test.go +++ b/providers/openapi/operation_test.go @@ -3,8 +3,6 @@ package openapi_test import ( "encoding/json" "fmt" - "github.com/stretchr/testify/require" - "gopkg.in/yaml.v3" "mokapi/config/dynamic" "mokapi/config/dynamic/dynamictest" "mokapi/providers/openapi" @@ -13,6 +11,9 @@ import ( "net/url" "strings" "testing" + + "github.com/stretchr/testify/require" + "gopkg.in/yaml.v3" ) func TestOperation_UnmarshalJSON(t *testing.T) { @@ -209,9 +210,9 @@ func TestOperation_Parse(t *testing.T) { return nil, nil }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation(http.MethodGet, &openapi.Operation{}), - )), + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodGet), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.NoError(t, err) @@ -226,11 +227,9 @@ func TestOperation_Parse(t *testing.T) { return nil, fmt.Errorf("TEST ERROR") }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( + openapitest.WithPath("/foo", openapitest.WithOperation(http.MethodGet, - openapitest.NewOperation( - openapitest.WithOperationParamRef(&openapi.ParameterRef{Reference: dynamic.Reference{Ref: "foo.yml"}}))), - )), + openapitest.WithOperationParamRef("foo.yml"))), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.EqualError(t, err, "parse path '/foo' failed: parse operation 'GET' failed: parse parameter index '0' failed: resolve reference 'foo.yml' failed: TEST ERROR") @@ -243,9 +242,9 @@ func TestOperation_Parse(t *testing.T) { return nil, nil }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation(http.MethodPost, &openapi.Operation{}), - )), + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodPost), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.NoError(t, err) @@ -260,11 +259,9 @@ func TestOperation_Parse(t *testing.T) { return nil, fmt.Errorf("TEST ERROR") }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( + openapitest.WithPath("/foo", openapitest.WithOperation(http.MethodPost, - openapitest.NewOperation( - openapitest.WithOperationParamRef(&openapi.ParameterRef{Reference: dynamic.Reference{Ref: "foo.yml"}}))), - )), + openapitest.WithOperationParamRef("foo.yml"))), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.EqualError(t, err, "parse path '/foo' failed: parse operation 'POST' failed: parse parameter index '0' failed: resolve reference 'foo.yml' failed: TEST ERROR") @@ -277,9 +274,9 @@ func TestOperation_Parse(t *testing.T) { return nil, nil }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation(http.MethodPut, &openapi.Operation{}), - )), + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodPut), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.NoError(t, err) @@ -294,11 +291,9 @@ func TestOperation_Parse(t *testing.T) { return nil, fmt.Errorf("TEST ERROR") }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( + openapitest.WithPath("/foo", openapitest.WithOperation(http.MethodPut, - openapitest.NewOperation( - openapitest.WithOperationParamRef(&openapi.ParameterRef{Reference: dynamic.Reference{Ref: "foo.yml"}}))), - )), + openapitest.WithOperationParamRef("foo.yml"))), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.EqualError(t, err, "parse path '/foo' failed: parse operation 'PUT' failed: parse parameter index '0' failed: resolve reference 'foo.yml' failed: TEST ERROR") @@ -311,9 +306,9 @@ func TestOperation_Parse(t *testing.T) { return nil, nil }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation(http.MethodPatch, &openapi.Operation{}), - )), + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodPatch), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.NoError(t, err) @@ -328,11 +323,9 @@ func TestOperation_Parse(t *testing.T) { return nil, fmt.Errorf("TEST ERROR") }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( + openapitest.WithPath("/foo", openapitest.WithOperation(http.MethodPatch, - openapitest.NewOperation( - openapitest.WithOperationParamRef(&openapi.ParameterRef{Reference: dynamic.Reference{Ref: "foo.yml"}}))), - )), + openapitest.WithOperationParamRef("foo.yml"))), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.EqualError(t, err, "parse path '/foo' failed: parse operation 'PATCH' failed: parse parameter index '0' failed: resolve reference 'foo.yml' failed: TEST ERROR") @@ -345,9 +338,9 @@ func TestOperation_Parse(t *testing.T) { return nil, nil }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation(http.MethodDelete, &openapi.Operation{}), - )), + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodDelete), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.NoError(t, err) @@ -362,11 +355,9 @@ func TestOperation_Parse(t *testing.T) { return nil, fmt.Errorf("TEST ERROR") }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( + openapitest.WithPath("/foo", openapitest.WithOperation(http.MethodDelete, - openapitest.NewOperation( - openapitest.WithOperationParamRef(&openapi.ParameterRef{Reference: dynamic.Reference{Ref: "foo.yml"}}))), - )), + openapitest.WithOperationParamRef("foo.yml"))), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.EqualError(t, err, "parse path '/foo' failed: parse operation 'DELETE' failed: parse parameter index '0' failed: resolve reference 'foo.yml' failed: TEST ERROR") @@ -379,9 +370,9 @@ func TestOperation_Parse(t *testing.T) { return nil, nil }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation(http.MethodHead, &openapi.Operation{}), - )), + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodHead), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.NoError(t, err) @@ -396,11 +387,9 @@ func TestOperation_Parse(t *testing.T) { return nil, fmt.Errorf("TEST ERROR") }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( + openapitest.WithPath("/foo", openapitest.WithOperation(http.MethodHead, - openapitest.NewOperation( - openapitest.WithOperationParamRef(&openapi.ParameterRef{Reference: dynamic.Reference{Ref: "foo.yml"}}))), - )), + openapitest.WithOperationParamRef("foo.yml"))), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.EqualError(t, err, "parse path '/foo' failed: parse operation 'HEAD' failed: parse parameter index '0' failed: resolve reference 'foo.yml' failed: TEST ERROR") @@ -413,9 +402,9 @@ func TestOperation_Parse(t *testing.T) { return nil, nil }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation(http.MethodOptions, &openapi.Operation{}), - )), + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodOptions), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.NoError(t, err) @@ -430,11 +419,9 @@ func TestOperation_Parse(t *testing.T) { return nil, fmt.Errorf("TEST ERROR") }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( + openapitest.WithPath("/foo", openapitest.WithOperation(http.MethodOptions, - openapitest.NewOperation( - openapitest.WithOperationParamRef(&openapi.ParameterRef{Reference: dynamic.Reference{Ref: "foo.yml"}}))), - )), + openapitest.WithOperationParamRef("foo.yml"))), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.EqualError(t, err, "parse path '/foo' failed: parse operation 'OPTIONS' failed: parse parameter index '0' failed: resolve reference 'foo.yml' failed: TEST ERROR") @@ -447,9 +434,9 @@ func TestOperation_Parse(t *testing.T) { return nil, nil }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation(http.MethodTrace, &openapi.Operation{}), - )), + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodTrace), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.NoError(t, err) @@ -464,11 +451,9 @@ func TestOperation_Parse(t *testing.T) { return nil, fmt.Errorf("TEST ERROR") }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( + openapitest.WithPath("/foo", openapitest.WithOperation(http.MethodTrace, - openapitest.NewOperation( - openapitest.WithOperationParamRef(&openapi.ParameterRef{Reference: dynamic.Reference{Ref: "foo.yml"}}))), - )), + openapitest.WithOperationParamRef("foo.yml"))), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.EqualError(t, err, "parse path '/foo' failed: parse operation 'TRACE' failed: parse parameter index '0' failed: resolve reference 'foo.yml' failed: TEST ERROR") @@ -481,9 +466,9 @@ func TestOperation_Parse(t *testing.T) { return nil, nil }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation("QUERY", &openapi.Operation{}), - )), + openapitest.WithPath("/foo", + openapitest.WithOperation("QUERY"), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.NoError(t, err) @@ -498,11 +483,9 @@ func TestOperation_Parse(t *testing.T) { return nil, fmt.Errorf("TEST ERROR") }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( + openapitest.WithPath("/foo", openapitest.WithOperation("QUERY", - openapitest.NewOperation( - openapitest.WithOperationParamRef(&openapi.ParameterRef{Reference: dynamic.Reference{Ref: "foo.yml"}}))), - )), + openapitest.WithOperationParamRef("foo.yml"))), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.EqualError(t, err, "parse path '/foo' failed: parse operation 'QUERY' failed: parse parameter index '0' failed: resolve reference 'foo.yml' failed: TEST ERROR") @@ -515,9 +498,9 @@ func TestOperation_Parse(t *testing.T) { return nil, nil }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation("LINK", &openapi.Operation{}), - )), + openapitest.WithPath("/foo", + openapitest.WithOperation("LINK"), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.NoError(t, err) @@ -532,11 +515,9 @@ func TestOperation_Parse(t *testing.T) { return nil, fmt.Errorf("TEST ERROR") }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( + openapitest.WithPath("/foo", openapitest.WithOperation("LINK", - openapitest.NewOperation( - openapitest.WithOperationParamRef(&openapi.ParameterRef{Reference: dynamic.Reference{Ref: "foo.yml"}}))), - )), + openapitest.WithOperationParamRef("foo.yml"))), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.EqualError(t, err, "parse path '/foo' failed: parse operation 'LINK' failed: parse parameter index '0' failed: resolve reference 'foo.yml' failed: TEST ERROR") @@ -549,11 +530,11 @@ func TestOperation_Parse(t *testing.T) { return nil, nil }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation(http.MethodTrace, &openapi.Operation{ + openapitest.WithPath("/foo", + openapitest.UseOperation(http.MethodTrace, &openapi.Operation{ RequestBody: &openapi.RequestBodyRef{Value: &openapi.RequestBody{Description: "foo"}}, }), - )), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.NoError(t, err) @@ -573,11 +554,11 @@ func TestOperation_Parse(t *testing.T) { return cfg, nil }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation(http.MethodTrace, &openapi.Operation{ + openapitest.WithPath("/foo", + openapitest.UseOperation(http.MethodTrace, &openapi.Operation{ RequestBody: &openapi.RequestBodyRef{Reference: dynamic.Reference{Ref: "foo.yml#/components/requestBodies/foo"}}, }), - )), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.NoError(t, err) @@ -592,11 +573,11 @@ func TestOperation_Parse(t *testing.T) { return nil, fmt.Errorf("TEST ERROR") }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation(http.MethodTrace, &openapi.Operation{ + openapitest.WithPath("/foo", + openapitest.UseOperation(http.MethodTrace, &openapi.Operation{ RequestBody: &openapi.RequestBodyRef{Reference: dynamic.Reference{Ref: "foo.yml#/components/requestBodies/foo"}}, }), - )), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.EqualError(t, err, "parse path '/foo' failed: parse operation 'TRACE' failed: parse request body failed: resolve reference 'foo.yml#/components/requestBodies/foo' failed: TEST ERROR") @@ -628,9 +609,9 @@ func TestConfig_Patch_Operation(t *testing.T) { name: "patch path with " + m, configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath())), + "/foo")), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation(m, openapitest.NewOperation())))), + "/foo", openapitest.WithOperation(m))), }, test: func(t *testing.T, result *openapi.Config) { require.Len(t, result.Paths, 1) @@ -642,11 +623,11 @@ func TestConfig_Patch_Operation(t *testing.T) { name: fmt.Sprintf("patch path %v info", m), configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation(m, openapitest.NewOperation())))), + "/foo", openapitest.WithOperation(m))), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation(m, openapitest.NewOperation( + "/foo", openapitest.WithOperation(m, openapitest.WithOperationInfo("foo", "bar", "id", true), - ))))), + ))), }, test: func(t *testing.T, result *openapi.Config) { require.Len(t, result.Paths, 1) @@ -664,9 +645,9 @@ func TestConfig_Patch_Operation(t *testing.T) { name: "patch is nil", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation("get", openapitest.NewOperation())))), + "/foo", openapitest.WithOperation("get"))), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath())), + "/foo")), }, test: func(t *testing.T, result *openapi.Config) { require.Len(t, result.Paths, 1) diff --git a/providers/openapi/parse_test.go b/providers/openapi/parse_test.go index aaf28890a..2c9ae2a10 100644 --- a/providers/openapi/parse_test.go +++ b/providers/openapi/parse_test.go @@ -18,34 +18,18 @@ func Test_Parse(t *testing.T) { c *openapi.Config test func(t *testing.T, c *openapi.Config, err error) }{ - { - name: "schemas reference", - c: openapitest.NewConfig("3.1", - openapitest.WithComponentSchema("Foo", schematest.New("array", schematest.WithItemsRef("#/components/schemas/Bar"))), - openapitest.WithComponentSchema("Bar", schematest.New("string")), - ), - test: func(t *testing.T, c *openapi.Config, err error) { - require.NoError(t, err) - foo := c.Components.Schemas.Get("Foo") - require.Equal(t, "array", foo.Type.String()) - bar := c.Components.Schemas.Get("Bar") - require.Equal(t, "string", bar.Type.String()) - // reference should point to same schema - require.Equal(t, bar, foo.Items.Sub) - }, - }, { name: "response schema reference should point to same objects", c: openapitest.NewConfig("3.1", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation("get", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("application/json", + openapitest.WithPath("/foo", + openapitest.WithOperation("get", + openapitest.WithResponse(200, openapitest.UseContent("application/json", &openapi.MediaType{ Schema: &schema.Schema{Ref: "#/components/schemas/Foo"}, }, - )), + ), + ), )), - )), openapitest.WithComponentSchema("Foo", schematest.New("array", schematest.WithItemsRef("#/components/schemas/Bar"))), openapitest.WithComponentSchema("Bar", schematest.New("string")), ), @@ -82,35 +66,19 @@ func Test_ParseAndPatch(t *testing.T) { configs []*openapi.Config test func(t *testing.T, c *openapi.Config) }{ - { - name: "schemas reference", - configs: []*openapi.Config{ - openapitest.NewConfig("3.1", - openapitest.WithComponentSchema("Foo", schematest.New("array", schematest.WithItemsRef("#/components/schemas/Bar"))), - openapitest.WithComponentSchema("Bar", schematest.New("string")), - ), - openapitest.NewConfig("3.1", - openapitest.WithComponentSchema("Bar", schematest.New("string", schematest.IsNullable(true))), - ), - }, - test: func(t *testing.T, c *openapi.Config) { - foo := c.Components.Schemas.Get("Foo") - require.True(t, foo.Items.Nullable) - }, - }, { name: "response schema reference should point to same objects", configs: []*openapi.Config{ openapitest.NewConfig("3.1", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation("get", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("application/json", + openapitest.WithPath("/foo", + openapitest.WithOperation("get", + openapitest.WithResponse(200, openapitest.UseContent("application/json", &openapi.MediaType{ Schema: &schema.Schema{Ref: "#/components/schemas/Foo"}, }, - )), + ), + ), )), - )), openapitest.WithComponentSchema("Foo", schematest.New("array", schematest.WithItemsRef("#/components/schemas/Bar"))), openapitest.WithComponentSchema("Bar", schematest.New("string")), ), diff --git a/providers/openapi/patch_test.go b/providers/openapi/patch_test.go index d9cf71804..91384b21b 100644 --- a/providers/openapi/patch_test.go +++ b/providers/openapi/patch_test.go @@ -1,11 +1,12 @@ package openapi_test import ( - "github.com/stretchr/testify/require" "mokapi/providers/openapi" "mokapi/providers/openapi/openapitest" "mokapi/providers/openapi/schema/schematest" "testing" + + "github.com/stretchr/testify/require" ) func TestConfig_Patch_Methods_RequestBody(t *testing.T) { @@ -18,15 +19,15 @@ func TestConfig_Patch_Methods_RequestBody(t *testing.T) { name: "description and required", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation(), + "/foo", openapitest.WithOperation( + "post", ), - ))), + )), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation(openapitest.WithRequestBody("foo", true)), - ), - ))), + "/foo", openapitest.WithOperation( + "post", openapitest.WithRequestBody("foo", true)), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { require.Equal(t, "foo", result.Paths["/foo"].Value.Post.RequestBody.Value.Description) @@ -37,15 +38,15 @@ func TestConfig_Patch_Methods_RequestBody(t *testing.T) { name: "patch description and required", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation(openapitest.WithRequestBody("", false)), - ), - ))), + "/foo", openapitest.WithOperation( + "post", openapitest.WithRequestBody("", false)), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation(openapitest.WithRequestBody("foo", true)), - ), - ))), + "/foo", openapitest.WithOperation( + "post", openapitest.WithRequestBody("foo", true)), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { require.Equal(t, "foo", result.Paths["/foo"].Value.Post.RequestBody.Value.Description) @@ -56,19 +57,19 @@ func TestConfig_Patch_Methods_RequestBody(t *testing.T) { name: "patch add content type", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithRequestBody("foo", true, - openapitest.WithRequestContent("text/plain", &openapi.MediaType{}))), - ), - ))), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithRequestBody("foo", true, + openapitest.WithRequestContent("text/plain", &openapi.MediaType{}))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithRequestBody("foo", true, - openapitest.WithRequestContent("application/json", &openapi.MediaType{}))), - ), - ))), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithRequestBody("foo", true, + openapitest.WithRequestContent("application/json", &openapi.MediaType{}))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { body := result.Paths["/foo"].Value.Post.RequestBody.Value @@ -80,20 +81,20 @@ func TestConfig_Patch_Methods_RequestBody(t *testing.T) { name: "add content type schema", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithRequestBody("foo", true, - openapitest.WithRequestContent("text/plain", &openapi.MediaType{}))), - ), - ))), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithRequestBody("foo", true, + openapitest.WithRequestContent("text/plain", &openapi.MediaType{}))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithRequestBody("foo", true, - openapitest.WithRequestContent("text/plain", - openapitest.NewContent(openapitest.WithSchema(schematest.New("number")))))), - ), - ))), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithRequestBody("foo", true, + openapitest.WithRequestContent("text/plain", + openapitest.NewContent(openapitest.WithSchema(schematest.New("number")))))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { body := result.Paths["/foo"].Value.Post.RequestBody.Value @@ -105,21 +106,21 @@ func TestConfig_Patch_Methods_RequestBody(t *testing.T) { name: "patch content type schema", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithRequestBody("foo", true, - openapitest.WithRequestContent("text/plain", - openapitest.NewContent(openapitest.WithSchema(schematest.New("number")))))), - ), - ))), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithRequestBody("foo", true, + openapitest.WithRequestContent("text/plain", + openapitest.NewContent(openapitest.WithSchema(schematest.New("number")))))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithRequestBody("foo", true, - openapitest.WithRequestContent("text/plain", - openapitest.NewContent(openapitest.WithSchema(schematest.New("number", schematest.WithFormat("double"))))))), - ), - ))), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithRequestBody("foo", true, + openapitest.WithRequestContent("text/plain", + openapitest.NewContent(openapitest.WithSchema(schematest.New("number", schematest.WithFormat("double"))))))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { body := result.Paths["/foo"].Value.Post.RequestBody.Value @@ -132,20 +133,20 @@ func TestConfig_Patch_Methods_RequestBody(t *testing.T) { name: "patch content type example", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithRequestBody("foo", true, - openapitest.WithRequestContent("text/plain", &openapi.MediaType{}))), - ), - ))), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithRequestBody("foo", true, + openapitest.WithRequestContent("text/plain", &openapi.MediaType{}))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithRequestBody("foo", true, - openapitest.WithRequestContent("text/plain", - openapitest.NewContent(openapitest.WithExample(12))))), - ), - ))), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithRequestBody("foo", true, + openapitest.WithRequestContent("text/plain", + openapitest.NewContent(openapitest.WithExampleValue(12))))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { body := result.Paths["/foo"].Value.Post.RequestBody.Value @@ -186,16 +187,16 @@ func TestConfig_Patch_Methods_RequestBody(t *testing.T) { name: "patch operation security", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation(), + "/foo", openapitest.WithOperation( + "post", ), - ))), + )), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithSecurity(map[string][]string{"foo": {}})), - ), - ))), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithSecurity(map[string][]string{"foo": {}})), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { require.Len(t, result.Paths["/foo"].Value.Post.Security, 1) @@ -206,18 +207,18 @@ func TestConfig_Patch_Methods_RequestBody(t *testing.T) { name: "patch operation security add scope", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithSecurity(map[string][]string{"foo": {"foo"}}), - ), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithSecurity(map[string][]string{"foo": {"foo"}}), ), - ))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithSecurity(map[string][]string{"foo": {"foo", "bar"}})), - ), - ))), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithSecurity(map[string][]string{"foo": {"foo", "bar"}})), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { require.Len(t, result.Paths["/foo"].Value.Post.Security, 2) diff --git a/providers/openapi/path.go b/providers/openapi/path.go index 56feae9a6..9114ce9a6 100644 --- a/providers/openapi/path.go +++ b/providers/openapi/path.go @@ -177,9 +177,9 @@ func (p *Path) parse(config *dynamic.Config, reader dynamic.Reader) error { return nil } - for _, param := range p.Parameters { + for index, param := range p.Parameters { if err := param.Parse(config, reader); err != nil { - return err + return fmt.Errorf("parse parameter '%v' failed: %w", index, err) } } diff --git a/providers/openapi/path_test.go b/providers/openapi/path_test.go index 4d437463b..f8b4454da 100644 --- a/providers/openapi/path_test.go +++ b/providers/openapi/path_test.go @@ -454,7 +454,7 @@ func TestPath_Parse(t *testing.T) { require.Equal(t, "/foo.yml", u.String()) cfg := &dynamic.Config{Info: dynamic.ConfigInfo{Url: u}, Data: openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", target)), + openapitest.UsePath("/foo", target)), } return cfg, nil }) @@ -473,7 +473,7 @@ func TestPath_Parse(t *testing.T) { require.Equal(t, "/foo.yml", u.String()) cfg := &dynamic.Config{Info: dynamic.ConfigInfo{Url: u}, Data: openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", nil)), + openapitest.UsePath("/foo", nil)), } return cfg, nil }) @@ -511,12 +511,12 @@ func TestPath_Parse(t *testing.T) { return nil, fmt.Errorf("TEST ERROR") }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithPathParamRef(&openapi.ParameterRef{Reference: dynamic.Reference{Ref: "foo.yml"}}), - )), + openapitest.WithPath("/foo", + openapitest.WithPathParamRef("foo.yml"), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) - require.EqualError(t, err, "parse path '/foo' failed: resolve reference 'foo.yml' failed: TEST ERROR") + require.EqualError(t, err, "parse path '/foo' failed: parse parameter '0' failed: resolve reference 'foo.yml' failed: TEST ERROR") }, }, { @@ -557,10 +557,10 @@ func TestConfig_Patch_Path(t *testing.T) { { name: "patch both path are nil", configs: []*openapi.Config{ - openapitest.NewConfig("1.0", openapitest.WithPath( + openapitest.NewConfig("1.0", openapitest.UsePath( "/foo", nil, )), - openapitest.NewConfig("1.0", openapitest.WithPath( + openapitest.NewConfig("1.0", openapitest.UsePath( "/foo", nil, )), }, @@ -575,12 +575,12 @@ func TestConfig_Patch_Path(t *testing.T) { configs: []*openapi.Config{ {Paths: map[string]*openapi.PathRef{"/foo": nil}}, openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath( - openapitest.WithOperation( - "post", openapitest.NewOperation(), - ), + "/foo", + openapitest.WithOperation( + "post", ), - )), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { require.Len(t, result.Paths, 1) @@ -592,10 +592,10 @@ func TestConfig_Patch_Path(t *testing.T) { name: "patch without path", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation(), + "/foo", openapitest.WithOperation( + "post", ), - ))), + )), openapitest.NewConfig("1.0"), }, test: func(t *testing.T, result *openapi.Config) { @@ -609,10 +609,10 @@ func TestConfig_Patch_Path(t *testing.T) { configs: []*openapi.Config{ openapitest.NewConfig("1.0"), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation(), + "/foo", openapitest.WithOperation( + "post", ), - ))), + )), }, test: func(t *testing.T, result *openapi.Config) { require.Len(t, result.Paths, 1) @@ -626,10 +626,10 @@ func TestConfig_Patch_Path(t *testing.T) { openapitest.NewConfig("1.0", openapitest.WithPathRef( "/foo", &openapi.PathRef{})), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation(), + "/foo", openapitest.WithOperation( + "post", ), - ))), + )), }, test: func(t *testing.T, result *openapi.Config) { require.Len(t, result.Paths, 1) @@ -641,9 +641,9 @@ func TestConfig_Patch_Path(t *testing.T) { name: "patch summary and description", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath())), + "/foo")), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithPathInfo("foo", "bar")))), + "/foo", openapitest.WithPathInfo("foo", "bar"))), }, test: func(t *testing.T, result *openapi.Config) { require.Len(t, result.Paths, 1) @@ -656,9 +656,9 @@ func TestConfig_Patch_Path(t *testing.T) { name: "add parameters", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath())), + "/foo")), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithPathParam("foo")))), + "/foo", openapitest.WithPathParam("foo"))), }, test: func(t *testing.T, result *openapi.Config) { e := result.Paths["/foo"].Value @@ -669,9 +669,9 @@ func TestConfig_Patch_Path(t *testing.T) { name: "patch parameters", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithPathParam("foo")))), + "/foo", openapitest.WithPathParam("foo"))), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithPathParam("foo", openapitest.WithParamSchema(schematest.New("number")))))), + "/foo", openapitest.WithPathParam("foo", openapitest.WithParamSchema(schematest.New("number"))))), }, test: func(t *testing.T, result *openapi.Config) { e := result.Paths["/foo"].Value diff --git a/providers/openapi/response.go b/providers/openapi/response.go index 5f4cb5ff8..08acce787 100644 --- a/providers/openapi/response.go +++ b/providers/openapi/response.go @@ -218,10 +218,10 @@ func (r *ResponseRef) parse(config *dynamic.Config, reader dynamic.Reader) error return dynamic.Resolve(r.Ref, &r.Value, config, reader) } - return r.Value.parse(config, reader) + return r.Value.Parse(config, reader) } -func (r *Response) parse(config *dynamic.Config, reader dynamic.Reader) error { +func (r *Response) Parse(config *dynamic.Config, reader dynamic.Reader) error { if r == nil { return nil } diff --git a/providers/openapi/response_test.go b/providers/openapi/response_test.go index a3d1cf1ef..62a3ca9a7 100644 --- a/providers/openapi/response_test.go +++ b/providers/openapi/response_test.go @@ -309,11 +309,11 @@ func TestResponse_Parse(t *testing.T) { return nil, nil }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation(http.MethodGet, openapitest.NewOperation( + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodGet, openapitest.WithResponse(http.StatusOK), - )), - )), + ), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.NoError(t, err) @@ -326,9 +326,9 @@ func TestResponse_Parse(t *testing.T) { return nil, nil }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation(http.MethodGet, &openapi.Operation{}), - )), + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodGet), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.NoError(t, err) @@ -341,11 +341,11 @@ func TestResponse_Parse(t *testing.T) { return nil, nil }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation(http.MethodGet, openapitest.NewOperation( - openapitest.WithResponseRef(http.StatusOK, nil), - )), - )), + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodGet, + openapitest.UseResponse(http.StatusOK, nil), + ), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.NoError(t, err) @@ -358,11 +358,11 @@ func TestResponse_Parse(t *testing.T) { return nil, fmt.Errorf("TEST ERROR") }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation(http.MethodGet, openapitest.NewOperation( - openapitest.WithResponseRef(http.StatusOK, &openapi.ResponseRef{Reference: dynamic.Reference{Ref: "foo.yml"}}), - )), - )), + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodGet, + openapitest.WithResponseRef(http.StatusOK, "foo.yml"), + ), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.EqualError(t, err, "parse path '/foo' failed: parse operation 'GET' failed: parse response '200' failed: resolve reference 'foo.yml' failed: TEST ERROR") @@ -375,11 +375,11 @@ func TestResponse_Parse(t *testing.T) { return nil, fmt.Errorf("TEST ERROR") }) config := openapitest.NewConfig("3.0", - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation(http.MethodGet, openapitest.NewOperation( - openapitest.WithResponse(http.StatusOK, openapitest.WithResponseHeaderRef("foo", &openapi.HeaderRef{Reference: dynamic.Reference{Ref: "foo.yml"}})), - )), - )), + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodGet, + openapitest.WithResponse(http.StatusOK, openapitest.WithResponseHeaderRef("foo", "foo.yml")), + ), + ), ) err := config.Parse(&dynamic.Config{Info: dynamic.ConfigInfo{Url: &url.URL{}}, Data: config}, reader) require.EqualError(t, err, "parse path '/foo' failed: parse operation 'GET' failed: parse response '200' failed: parse header 'foo' failed: resolve reference 'foo.yml' failed: TEST ERROR") @@ -415,20 +415,20 @@ func TestConfig_Patch_Response(t *testing.T) { name: "patch is nil", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithResponseDescription("foo"))), - ), - ))), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.WithResponseDescription("foo"))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( + "/foo", openapitest.UseOperation( "post", &openapi.Operation{ Responses: &openapi.Responses{LinkedHashMap: getResponses(map[string]*openapi.ResponseRef{ "200": nil, })}, }), - ), - )), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) @@ -439,16 +439,16 @@ func TestConfig_Patch_Response(t *testing.T) { name: "add response", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", &openapi.Operation{}, + "/foo", openapitest.WithOperation( + "post", ), - ))), + )), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithResponseDescription("foo"))), - ), - ))), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.WithResponseDescription("foo"))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) @@ -459,17 +459,17 @@ func TestConfig_Patch_Response(t *testing.T) { name: "patch response", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(204, openapitest.WithResponseDescription("bar"))), - ), - ))), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(204, openapitest.WithResponseDescription("bar"))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithResponseDescription("foo"))), - ), - ))), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.WithResponseDescription("foo"))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(204) @@ -482,15 +482,15 @@ func TestConfig_Patch_Response(t *testing.T) { name: "patch is nil", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(204, openapitest.WithResponseDescription("bar"))), - ), - ))), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(204, openapitest.WithResponseDescription("bar"))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", &openapi.Operation{}), - ))), + "/foo", openapitest.WithOperation( + "post"), + )), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(204) @@ -501,17 +501,17 @@ func TestConfig_Patch_Response(t *testing.T) { name: "patch response is nil", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(204, openapitest.WithResponseDescription("bar"))), - ), - ))), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(204, openapitest.WithResponseDescription("bar"))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponseRef(202, &openapi.ResponseRef{}), - ), - )))), + "/foo", openapitest.WithOperation( + "post", + openapitest.UseResponse(202, nil), + ), + )), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(202) @@ -522,17 +522,17 @@ func TestConfig_Patch_Response(t *testing.T) { name: "patch description", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200)), - ), - ))), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200)), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithResponseDescription("foo"))), - ), - ))), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.WithResponseDescription("foo"))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) @@ -543,17 +543,17 @@ func TestConfig_Patch_Response(t *testing.T) { name: "patch add content type", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/plain", openapitest.NewContent()))), - ), - ))), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.WithContent("text/plain"))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("application/json", openapitest.NewContent()))), - ), - ))), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.WithContent("application/json"))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) @@ -565,19 +565,18 @@ func TestConfig_Patch_Response(t *testing.T) { name: "add content type schema", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/plain", openapitest.NewContent()))), - ), - ))), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.WithContent("text/plain"))), + ), + ), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/plain", - openapitest.NewContent( - openapitest.WithSchema(schematest.New("number")))))), - ), - ))), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.WithContent("text/plain", + openapitest.WithSchema(schematest.New("number"))))), + ), + ), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) @@ -589,21 +588,18 @@ func TestConfig_Patch_Response(t *testing.T) { name: "patch content type schema", configs: []*openapi.Config{ openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/plain", - openapitest.NewContent( - openapitest.WithSchema(schematest.New("number")))), - ), - ))))), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.WithContent("text/plain", + + openapitest.WithSchema(schematest.New("number")))), + ))), openapitest.NewConfig("1.0", openapitest.WithPath( - "/foo", openapitest.NewPath(openapitest.WithOperation( - "post", openapitest.NewOperation( - openapitest.WithResponse(200, openapitest.WithContent("text/plain", - openapitest.NewContent( - openapitest.WithSchema(schematest.New("number", schematest.WithFormat("double")))), - ), - )))))), + "/foo", openapitest.WithOperation( + "post", + openapitest.WithResponse(200, openapitest.WithContent("text/plain", + openapitest.WithSchema(schematest.New("number", schematest.WithFormat("double")))), + )))), }, test: func(t *testing.T, result *openapi.Config) { res := result.Paths["/foo"].Value.Post.Responses.GetResponse(200) diff --git a/runtime/index_test.go b/runtime/index_test.go index dd918b623..01e5e816c 100644 --- a/runtime/index_test.go +++ b/runtime/index_test.go @@ -34,11 +34,11 @@ func TestIndex(t *testing.T) { require.NoError(t, err) petstore := openapitest.NewConfig("3.0", openapitest.WithInfo("Swagger Petstore", "1.0", "This is a sample server Petstore server. You can find out more about Swagger at https://swagger.io"), - openapitest.WithPath("/pet", openapitest.NewPath( - openapitest.WithOperation("put", openapitest.NewOperation( + openapitest.WithPath("/pet", + openapitest.WithOperation("put", openapitest.WithOperationInfo("Update an existing pet.", "Update an existing pet by Id.", "updatePet", false), - )), - )), + ), + ), ) app.AddHttp(&dynamic.Config{ Info: dynamic.ConfigInfo{Url: try.MustUrl("petstore.yaml"), Provider: "NPM"}, diff --git a/runtime/runtime_http_search_test.go b/runtime/runtime_http_search_test.go index 74494a172..365afeab4 100644 --- a/runtime/runtime_http_search_test.go +++ b/runtime/runtime_http_search_test.go @@ -147,7 +147,7 @@ func TestIndex_Http(t *testing.T) { test: func(t *testing.T, app *runtime.App) { cfg := openapitest.NewConfig("3.0", openapitest.WithInfo("foo", "1.0", ""), - openapitest.WithPath("/pets", openapitest.NewPath()), + openapitest.WithPath("/pets"), ) app.AddHttp(toConfig(cfg)) @@ -179,7 +179,7 @@ func TestIndex_Http(t *testing.T) { test: func(t *testing.T, app *runtime.App) { cfg := openapitest.NewConfig("3.0", openapitest.WithInfo("foo", "1.0", "a description"), - openapitest.WithPath("/pets", openapitest.NewPath(openapitest.WithPathInfo("", "a description"))), + openapitest.WithPath("/pets", openapitest.WithPathInfo("", "a description")), ) app.AddHttp(toConfig(cfg)) @@ -222,12 +222,12 @@ func TestIndex_Http(t *testing.T) { test: func(t *testing.T, app *runtime.App) { cfg := openapitest.NewConfig("3.0", openapitest.WithInfo("foo", "1.0", "a description"), - openapitest.WithPath("/pets", openapitest.NewPath( + openapitest.WithPath("/pets", openapitest.WithPathInfo("", "a description"), - openapitest.WithOperation("get", openapitest.NewOperation( + openapitest.WithOperation("get", openapitest.WithHeaderParam("foo", true, openapitest.WithParamInfo("parameter description")), - )), - )), + ), + ), ) app.AddHttp(toConfig(cfg)) @@ -259,12 +259,12 @@ func TestIndex_Http(t *testing.T) { name: "Search by api field", test: func(t *testing.T, app *runtime.App) { cfg := openapitest.NewConfig("3.0", openapitest.WithInfo("Petstore", "", ""), - openapitest.WithPath("/pets", openapitest.NewPath( + openapitest.WithPath("/pets", openapitest.WithPathInfo("", "path"), - openapitest.WithOperation("get", openapitest.NewOperation( + openapitest.WithOperation("get", openapitest.WithHeaderParam("foo", true, openapitest.WithParamInfo("parameter")), - )), - )), + ), + ), ) app.AddHttp(toConfig(cfg)) // search response should only have one the root OpenAPI object @@ -325,12 +325,12 @@ func TestIndex_Http(t *testing.T) { name: "Search by api field", test: func(t *testing.T, app *runtime.App) { cfg := openapitest.NewConfig("3.0", openapitest.WithInfo("Petstore", "", ""), - openapitest.WithPath("/pets", openapitest.NewPath( + openapitest.WithPath("/pets", openapitest.WithPathInfo("", "path"), - openapitest.WithOperation("get", openapitest.NewOperation( + openapitest.WithOperation("get", openapitest.WithHeaderParam("foo", true, openapitest.WithParamInfo("parameter")), - )), - )), + ), + ), ) app.AddHttp(toConfig(cfg)) // search response should only have one the root OpenAPI object @@ -364,12 +364,12 @@ func TestIndex_Http(t *testing.T) { name: "Search fuzzy", test: func(t *testing.T, app *runtime.App) { cfg := openapitest.NewConfig("3.0", openapitest.WithInfo("Petstore", "", ""), - openapitest.WithPath("/pets", openapitest.NewPath( + openapitest.WithPath("/pets", openapitest.WithPathInfo("", "path"), - openapitest.WithOperation("get", openapitest.NewOperation( + openapitest.WithOperation("get", openapitest.WithHeaderParam("foo", true, openapitest.WithParamInfo("parameter")), - )), - )), + ), + ), ) app.AddHttp(toConfig(cfg)) // search response should only have one the root OpenAPI object diff --git a/runtime/runtime_http_test.go b/runtime/runtime_http_test.go index c65a8ec95..bac8a8f6c 100644 --- a/runtime/runtime_http_test.go +++ b/runtime/runtime_http_test.go @@ -1,7 +1,6 @@ package runtime_test import ( - "github.com/stretchr/testify/require" "mokapi/config/dynamic" "mokapi/config/static" "mokapi/engine/enginetest" @@ -15,6 +14,8 @@ import ( "net/http/httptest" "net/url" "testing" + + "github.com/stretchr/testify/require" ) func TestApp_AddHttp(t *testing.T) { @@ -36,7 +37,7 @@ func TestApp_AddHttp(t *testing.T) { name: "event store for endpoint available", test: func(t *testing.T, app *runtime.App) { app.AddHttp(newConfig(openapitest.NewConfig("3.0", openapitest.WithInfo("foo", "", ""), - openapitest.WithPath("bar", openapitest.NewPath())))) + openapitest.WithPath("bar")))) require.NotNil(t, app.GetHttp("foo")) err := app.Events.Push(&eventstest.Event{Name: "bar"}, events.NewTraits().WithNamespace("http").WithName("foo").With("path", "bar")) @@ -47,11 +48,11 @@ func TestApp_AddHttp(t *testing.T) { name: "request is counted in monitor", test: func(t *testing.T, app *runtime.App) { info := app.AddHttp(newConfig(openapitest.NewConfig("3.0", openapitest.WithInfo("foo", "", ""), - openapitest.WithPath("/foo", openapitest.NewPath( - openapitest.WithOperation(http.MethodGet, openapitest.NewOperation( + openapitest.WithPath("/foo", + openapitest.WithOperation(http.MethodGet, openapitest.WithResponse(http.StatusOK), - )))), - ))) + ))), + )) m := monitor.NewHttp() h := info.Handler(m, enginetest.NewEngine(), &events.StoreManager{}) @@ -66,7 +67,7 @@ func TestApp_AddHttp(t *testing.T) { name: "retrieve configs", test: func(t *testing.T, app *runtime.App) { info := app.AddHttp(newConfig(openapitest.NewConfig("3.0", openapitest.WithInfo("foo", "", ""), - openapitest.WithPath("bar", openapitest.NewPath())))) + openapitest.WithPath("bar")))) configs := info.Configs() require.Len(t, configs, 1) diff --git a/server/httpmanager_test.go b/server/httpmanager_test.go index 58779965c..eb27c84bb 100644 --- a/server/httpmanager_test.go +++ b/server/httpmanager_test.go @@ -35,7 +35,7 @@ func TestHttpServers_Monitor(t *testing.T) { port := try.GetFreePort() u := fmt.Sprintf("http://localhost:%v", port) c := openapitest.NewConfig("3.0", openapitest.WithInfo("test", "1.0", ""), openapitest.WithServer(u, "")) - openapitest.AppendPath("/foo", c, openapitest.WithOperation("get", openapitest.NewOperation())) + openapitest.AppendPath("/foo", c, openapitest.WithOperation("get")) //c := &openapi.Config{OpenApi: "3.0", Info: openapi.Info{Name: "foo"}, Servers: []*openapi.Server{{Url: url}}} m.Update(dynamic.ConfigEvent{Config: &dynamic.Config{Info: dynamic.ConfigInfo{Url: MustParseUrl("foo.yml")}, Data: c}}) diff --git a/server/server_http_test.go b/server/server_http_test.go index 4809b4f79..119de3717 100644 --- a/server/server_http_test.go +++ b/server/server_http_test.go @@ -57,10 +57,10 @@ func TestHttp(t *testing.T) { Config: &dynamic.Config{ Info: dynamictest.NewConfigInfo(), Data: openapitest.NewConfig("3.1.0", - openapitest.WithPath("/", openapitest.NewPath( - openapitest.WithOperation("GET", openapitest.NewOperation( - openapitest.WithResponse(200))), - )), + openapitest.WithPath("/", + openapitest.WithOperation("GET", + openapitest.WithResponse(200)), + ), ), }, }) @@ -79,10 +79,10 @@ func TestHttp(t *testing.T) { Config: &dynamic.Config{ Info: dynamictest.NewConfigInfo(), Data: openapitest.NewConfig("3.1.0", - openapitest.WithPath("/", openapitest.NewPath( - openapitest.WithOperation("GET", openapitest.NewOperation( - openapitest.WithResponse(200))), - )), + openapitest.WithPath("/", + openapitest.WithOperation("GET", + openapitest.WithResponse(200)), + ), ), }, }) @@ -116,10 +116,10 @@ func TestHttp(t *testing.T) { Config: &dynamic.Config{ Info: dynamictest.NewConfigInfo(), Data: openapitest.NewConfig("3.1.0", - openapitest.WithPath("/", openapitest.NewPath( - openapitest.WithOperation("GET", openapitest.NewOperation( - openapitest.WithResponse(200))), - )), + openapitest.WithPath("/", + openapitest.WithOperation("GET", + openapitest.WithResponse(200)), + ), openapitest.WithServer("/", ""), ), }, @@ -128,10 +128,10 @@ func TestHttp(t *testing.T) { Config: &dynamic.Config{ Info: dynamictest.NewConfigInfo(), Data: openapitest.NewConfig("3.1.0", - openapitest.WithPath("/", openapitest.NewPath( - openapitest.WithOperation("GET", openapitest.NewOperation( - openapitest.WithResponse(200))), - )), + openapitest.WithPath("/", + openapitest.WithOperation("GET", + openapitest.WithResponse(200)), + ), openapitest.WithServer("/foo", ""), ), }, @@ -156,10 +156,10 @@ func TestHttp(t *testing.T) { Config: &dynamic.Config{ Info: dynamictest.NewConfigInfo(), Data: openapitest.NewConfig("3.1.0", - openapitest.WithPath("/", openapitest.NewPath( - openapitest.WithOperation("GET", openapitest.NewOperation( - openapitest.WithResponse(200))), - )), + openapitest.WithPath("/", + openapitest.WithOperation("GET", + openapitest.WithResponse(200)), + ), openapitest.WithServer("/foo", ""), ), }, @@ -169,10 +169,10 @@ func TestHttp(t *testing.T) { Config: &dynamic.Config{ Info: dynamictest.NewConfigInfo(), Data: openapitest.NewConfig("3.1.0", - openapitest.WithPath("/", openapitest.NewPath( - openapitest.WithOperation("GET", openapitest.NewOperation( - openapitest.WithResponse(200))), - )), + openapitest.WithPath("/", + openapitest.WithOperation("GET", + openapitest.WithResponse(200)), + ), openapitest.WithServer(fmt.Sprintf("http://:%v/foo", port), ""), ), }, @@ -192,10 +192,10 @@ func TestHttp(t *testing.T) { Config: &dynamic.Config{ Info: dynamictest.NewConfigInfo(), Data: openapitest.NewConfig("3.1.0", - openapitest.WithPath("/", openapitest.NewPath( - openapitest.WithOperation("GET", openapitest.NewOperation( - openapitest.WithResponse(200))), - )), + openapitest.WithPath("/", + openapitest.WithOperation("GET", + openapitest.WithResponse(200)), + ), openapitest.WithServer("/foo", ""), ), }, @@ -205,10 +205,10 @@ func TestHttp(t *testing.T) { Config: &dynamic.Config{ Info: dynamictest.NewConfigInfo(), Data: openapitest.NewConfig("3.1.0", - openapitest.WithPath("/", openapitest.NewPath( - openapitest.WithOperation("GET", openapitest.NewOperation( - openapitest.WithResponse(200))), - )), + openapitest.WithPath("/", + openapitest.WithOperation("GET", + openapitest.WithResponse(200)), + ), openapitest.WithServer("/foo", ""), ), }, @@ -233,13 +233,13 @@ func TestHttp(t *testing.T) { Config: &dynamic.Config{ Info: dynamictest.NewConfigInfo(), Data: openapitest.NewConfig("3.1.0", - openapitest.WithPath("/", openapitest.NewPath( - openapitest.WithOperation("GET", openapitest.NewOperation( + openapitest.WithPath("/", + openapitest.WithOperation("GET", openapitest.WithResponse(200, openapitest.WithContent( "application/json", - openapitest.NewContent(openapitest.WithSchema(schematest.New("string", schematest.WithConst("foo")))), - )))), - )), + openapitest.WithSchema(schematest.New("string", schematest.WithConst("foo")))), + )), + ), ), }, }) From 32583e78a22095d03adef9280cb021b251b5ceba Mon Sep 17 00:00:00 2001 From: maesi Date: Tue, 10 Mar 2026 22:15:14 +0100 Subject: [PATCH 21/21] doc: update documentation and styles --- docs/get-started/installation.md | 106 +++++++++++++++++++++++++------ webui/src/views/DocsView.vue | 8 +++ 2 files changed, 94 insertions(+), 20 deletions(-) diff --git a/docs/get-started/installation.md b/docs/get-started/installation.md index f9dec24f8..fddd823b1 100644 --- a/docs/get-started/installation.md +++ b/docs/get-started/installation.md @@ -1,13 +1,41 @@ --- title: "Install Mokapi: Quick & Easy Setup Guide" -description: Learn how to install Mokapi effortlessly across Windows, macOS, and Linux. Follow the step-by-step guide for a smooth setup experience. +description: Install Mokapi on Windows, macOS, Linux, Docker, or Node.js. Follow a simple step-by-step guide to get started quickly. +subtitle: Get Mokapi running in seconds on Windows, macOS, Linux, Docker, or Node.js. +cards: + items: + - title: Run Your First Mock + href: /docs/get-started/running + description: Learn how to start Mokapi and mock your first API + - title: Explore Tutorials + href: /resources + description: Follow step-by-step guides for REST, Kafka, LDAP, and SMTP + - title: Write Scripts + href: /docs/javascript-api/overview + description: Add dynamic behavior to your mocks with JavaScript + - title: Configure Mokapi + href: /docs/configuration/overview + description: Customize ports, providers, and other settings --- # Install Mokapi -Mokapi is an open-source tool designed to simplify API mocking and schema validation. -It enables developers to prototype, test, and demonstrate APIs with realistic data and -scenarios. This guide provides straightforward instructions to install Mokapi on various -platforms. +## Overview + +Mokapi is an open-source API mocking tool that helps you develop and test faster by simulating REST APIs, Kafka topics, +LDAP directories, and SMTP servers. This guide shows you how to install Mokapi on your platform. + +Choose your preferred installation method based on your platform and workflow. + +```` box=benefits title="Try Without Installing" +Test Mokapi instantly with npx (requires Node.js): + +```bash style=simple +npx go-mokapi serve https://petstore3.swagger.io/api/v3/openapi.json +``` + + +This starts a mock server immediately without permanent installation. Perfect for quick tests and demos. +```` ## Installation Options @@ -16,10 +44,23 @@ Choose your preferred method below: ::: tabs +@tab "NPM" + +If you prefer to install Mokapi globally as a Node.js package, install [go-mokapi](https://www.npmjs.com/package/go-mokapi) +using: + +```bash +npm install -g go-mokapi +``` + +After installation, the mokapi command is available globally. + @tab "macOS" ### Homebrew +Install via Homebrew for easy updates and management: + ```bash brew tap marle3003/tap brew install mokapi @@ -27,12 +68,15 @@ brew install mokapi ### Direct Download -Download the latest macOS version from [GitHub](https://github.com/marle3003/mokapi/releases) +Download the latest macOS binary from [GitHub](https://github.com/marle3003/mokapi/releases). Extract +archive and move the binary to your PATH. @tab "Windows" ### Chocolatey +Install via Chocolatey for easy updates: + ```Powershell choco install mokapi ``` @@ -43,6 +87,8 @@ Download the latest Windows version from [GitHub](https://github.com/marle3003/m @tab "Linux" +Download the .deb package from the releases page and install: + ### Direct Download Download file appropriate for your Linux distribution and ARCH from the [release page](https://github.com/marle3003/mokapi/releases), then install with @@ -57,34 +103,54 @@ rpm -i mokapi_{version}_linux_{arch}.rpm @tab "Docker" -To get started with Mokapi using Docker, visit [DockerHub](https://hub.docker.com/r/mokapi/mokapi/tags) for a list of available images. -You can also use a custom base Docker image as demonstrated in [these examples](/resources/examples/mokapi-with-custom-base-image.md). +Mokapi provides official Docker images on [Docker Hub](https://hub.docker.com/r/mokapi/mokapi): ``` docker pull mokapi/mokapi ``` -@tab "NPM" +::: -If you prefer to install Mokapi as a Node.js package, install [go-mokapi](https://www.npmjs.com/package/go-mokapi) -using: +```` box=info title="Verify Installation" +After installation, verify Mokapi is working: -```bash -npm install -g go-mokapi +```bash style=simple +mokapi --version ``` -::: +```` -### Mokapi Scripts Type Definitions +## TypeScript Support -Mokapi allows you to write **custom scripts** to handle API events or modify responses. -For full type safety and autocompletion in TypeScript, you can install the [`@types/mokapi`](https://www.npmjs.com/package/@types/mokapi`) package: +For full type safety and autocompletion when writing Mokapi scripts in TypeScript, install the type definitions: ```bash npm install --save-dev @types/mokapi ``` -## Next steps +This enables IntelliSense and type checking in your IDE when writing custom event handlers and scripts. + +### Example TypeScript Script + +```typescript title=petstore.ts +import { on } from 'mokapi' + +export default function() { + on('http', (request, response) => { + // TypeScript provides full autocompletion here + if (request.path.petId === '999') { + response.statusCode = 404 + response.data = { message: 'Pet not found' } + } + }) +} +``` +``` box=tip title="IDE Integration" +With @types/mokapi installed, editors like VS Code will provide autocompletion for Mokapi's API, making script development faster and less error-prone. +``` + +## What's Next? + +Now that Mokapi is installed, here's what you can do: -- [Create your first Mock](/docs/get-started/running.md) -- [Install @types/mokapi](https://www.npmjs.com/package/@types/mokapi) \ No newline at end of file +{{ card-grid key="cards" }} \ No newline at end of file diff --git a/webui/src/views/DocsView.vue b/webui/src/views/DocsView.vue index b67a68ea2..2bb2231d1 100644 --- a/webui/src/views/DocsView.vue +++ b/webui/src/views/DocsView.vue @@ -599,6 +599,11 @@ a[name] { background-color: var(--card-background); } +.box.benefits .box-heading { + color: var(--color-green); + font-size: 1.1rem; +} + .box.feature { margin-top: 1rem; margin-bottom: 2rem; @@ -683,6 +688,9 @@ pre.simple { pre.simple code { padding: 0; } +pre.simple:has(+ p) { + margin-bottom: 12px; +} pre::before { content: attr(data-label); position: absolute;