From 302224faf2265369caaf3efecb74605f03130b16 Mon Sep 17 00:00:00 2001 From: shaniah868 Date: Thu, 24 Feb 2022 17:34:15 -0400 Subject: [PATCH 1/4] feature: WEOS-13065 Generate and Ensure Bdd Test is passing -Updated the feature file -Generated the bdd test --- controllers/rest/controller_standard.go | 74 +++++++++++++------- controllers/rest/controller_standard_test.go | 2 +- end2end_test.go | 37 +++++++++- features/default-response.feature | 6 +- 4 files changed, 89 insertions(+), 30 deletions(-) diff --git a/controllers/rest/controller_standard.go b/controllers/rest/controller_standard.go index c19e292b..0ed96cb5 100644 --- a/controllers/rest/controller_standard.go +++ b/controllers/rest/controller_standard.go @@ -671,55 +671,77 @@ func DefaultResponseMiddleware(api *RESTAPI, projection projections.Projection, return func(next echo.HandlerFunc) echo.HandlerFunc { return func(ctxt echo.Context) error { ctx := ctxt.Request().Context() + //take media type from the request since the context wouldnt add it because there is no entity factory to use + mediaType := ctxt.Request().Header.Get(weoscontext.ACCEPT) + var bytesArray []byte + var err error + contentType := "" + var respCode int + found := false for code, resp := range operation.Responses { - respCode, _ := strconv.Atoi(code) + if found { + break + } + respCode, _ = strconv.Atoi(code) if resp.Value.Content != nil { - //check for if there is one mediatype - if len(resp.Value.Content) == 1 { - for mediaType, content := range resp.Value.Content { + //check for if there is one mediatype or if h=there is no accept value + if len(resp.Value.Content) == 1 || mediaType == "" { + for key, content := range resp.Value.Content { if content.Example != nil { - var bytesArray []byte - bytesArray, err := json.Marshal(content.Example) + bytesArray, err = json.Marshal(content.Example) if err != nil { - api.e.Logger.Errorf("unexpected error %s ", err) + api.e.Logger.Debugf("unexpected error %s ", err) return NewControllerError(fmt.Sprintf("unexpected error %s ", err), err, http.StatusBadRequest) } - return ctxt.Blob(respCode, mediaType, bytesArray) + contentType = key + found = true + break } } //check for if there are multiple mediatype } else if len(resp.Value.Content) > 1 { - //take media type from the request since the context wouldnt add it because there is no entity factory to use - mediaType := ctxt.Request().Header.Get(weoscontext.ACCEPT) - if mediaType == "" { - api.e.Logger.Debugf("unexpected error accept header was not found") - return NewControllerError("unexpected error accept header was not found", nil, http.StatusBadRequest) - } if resp.Value.Content[mediaType] == nil { - api.e.Logger.Debugf("unexpected error %s media type not found", mediaType) - return NewControllerError(fmt.Sprintf("unexpected error %s media type not found", mediaType), nil, http.StatusBadRequest) + if strings.Contains(mediaType, "*") { + mediaT := strings.Replace(mediaType, "*", "", -1) + if resp.Value.Content[mediaT] != nil && resp.Value.Content[mediaT].Example != nil { + bytesArray, err = json.Marshal(resp.Value.Content[mediaType].Example) + if err != nil { + api.e.Logger.Debugf("unexpected error %s ", err) + return NewControllerError(fmt.Sprintf("unexpected error %s ", err), err, http.StatusBadRequest) + } + contentType = mediaType + found = true + break + } + } + //search all the responses to find if the mediatype is there + continue } else { if resp.Value.Content[mediaType].Example != nil { - var bytesArray []byte - bytesArray, err := json.Marshal(resp.Value.Content[mediaType].Example) + bytesArray, err = json.Marshal(resp.Value.Content[mediaType].Example) if err != nil { - api.e.Logger.Errorf("unexpected error %s ", err) + api.e.Logger.Debugf("unexpected error %s ", err) return NewControllerError(fmt.Sprintf("unexpected error %s ", err), err, http.StatusBadRequest) - } - //Add response to context for controller - ctx = context.WithValue(ctx, "resp", ctxt.Blob(respCode, mediaType, bytesArray)) - request := ctxt.Request().WithContext(ctx) - ctxt.SetRequest(request) - return next(ctxt) + contentType = mediaType + found = true + break } } } } } - return NewControllerError("unexpected error all responses were parsed, nothing was found", nil, http.StatusBadRequest) + if found { + //Add response to context for controller + ctx = context.WithValue(ctx, "resp", ctxt.Blob(respCode, contentType, bytesArray)) + request := ctxt.Request().WithContext(ctx) + ctxt.SetRequest(request) + return next(ctxt) + } + + return NewControllerError(fmt.Sprintf("unexpected error all responses were parsed,content type %s was not found", mediaType), nil, http.StatusBadRequest) } } diff --git a/controllers/rest/controller_standard_test.go b/controllers/rest/controller_standard_test.go index e05b7e03..5b446caa 100644 --- a/controllers/rest/controller_standard_test.go +++ b/controllers/rest/controller_standard_test.go @@ -2041,7 +2041,7 @@ func TestStandardControllers_DefaultResponse(t *testing.T) { response := resp.Result() defer response.Body.Close() - if response.StatusCode != http.StatusBadRequest { + if response.StatusCode != http.StatusOK { t.Errorf("expected response code to be %d, got %d", http.StatusBadRequest, response.StatusCode) } }) diff --git a/end2end_test.go b/end2end_test.go index 9b7a005a..02b3f590 100644 --- a/end2end_test.go +++ b/end2end_test.go @@ -7,6 +7,7 @@ import ( "encoding/json" "errors" "fmt" + "io" "mime/multipart" "net/http" "net/http/httptest" @@ -1459,6 +1460,35 @@ func theSwaggerUiShouldBeShown() error { return nil } +func theContentTypeShouldBe(mediaType string) error { + if rec.Header().Get("Content-Type") != mediaType { + return fmt.Errorf("expect content type to be %s got %s", mediaType, rec.Header().Get("Content-Type")) + } + return nil +} + +func theHeaderIsSetWithValue(key, value string) error { + + return godog.ErrPending +} + +func theResponseBodyShouldBe(expectResp *godog.DocString) error { + defer rec.Result().Body.Close() + var actualResp string + results, err := io.ReadAll(rec.Result().Body) + if err != nil { + return err + } + err = json.Unmarshal(results, &actualResp) + if err != nil { + return err + } + if actualResp != expectResp.Content { + return fmt.Errorf("expected response to be %s, got %s", expectResp.Content, actualResp) + } + return nil +} + func InitializeScenario(ctx *godog.ScenarioContext) { ctx.Before(reset) ctx.After(func(ctx context.Context, sc *godog.Scenario, err error) (context.Context, error) { @@ -1539,6 +1569,9 @@ func InitializeScenario(ctx *godog.ScenarioContext) { ctx.Step(`^the total no\. events and processed and failures should be returned$`, theTotalNoEventsAndProcessedAndFailuresShouldBeReturned) ctx.Step(`^the api as json should be shown$`, theApiAsJsonShouldBeShown) ctx.Step(`^the swagger ui should be shown$`, theSwaggerUiShouldBeShown) + ctx.Step(`^the content type should be "([^"]*)"$`, theContentTypeShouldBe) + ctx.Step(`^the header "([^"]*)" is set with value "([^"]*)"$`, theHeaderIsSetWithValue) + ctx.Step(`^the response body should be$`, theResponseBodyShouldBe) } @@ -1549,8 +1582,8 @@ func TestBDD(t *testing.T) { TestSuiteInitializer: InitializeSuite, Options: &godog.Options{ Format: "pretty", - Tags: "~long && ~skipped", - //Tags: "focus1", + //Tags: "~long && ~skipped", + Tags: "focus", //Tags: "WEOS-1110 && ~skipped", }, }.Run() diff --git a/features/default-response.feature b/features/default-response.feature index 0c140c1b..1bbb781b 100644 --- a/features/default-response.feature +++ b/features/default-response.feature @@ -26,6 +26,7 @@ Feature: Hardcode the response for an endpoint example: | TestThis is a test page """ + And the service is running When the "GET" endpoint "/" is hit Then a 200 response should be returned And the content type should be "text/html" @@ -55,14 +56,16 @@ Feature: Hardcode the response for an endpoint example: | Page Not FoundSome not found page """ + And the service is running When the "GET" endpoint "/" is hit Then a 200 response should be returned And the content type should be "text/html" And the response body should be """ TestThis is a test page + """ - @WEOS-1365 + @WEOS-1365 @focus Scenario: Send Accept header to hit at content of expected response If there are multiple content types you can send an [Accept header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept) @@ -84,6 +87,7 @@ Feature: Hardcode the response for an endpoint example: | Test """ + And the service is running And the header "Accept" is set with value "application/*" When the "GET" endpoint "/" is hit Then a 200 response should be returned From 606a934fb80e9fd9c08996d0905704fdab2ba81c Mon Sep 17 00:00:00 2001 From: shaniah868 Date: Thu, 24 Feb 2022 17:47:05 -0400 Subject: [PATCH 2/4] feature: WEOS-13065 Generate and Ensure Bdd Test is passing -Updated the feature file -Generated the bdd test -Update the controller functionality --- controllers/rest/controller_standard.go | 26 +++++++++++++++---------- end2end_test.go | 6 +++--- features/default-response.feature | 2 +- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/controllers/rest/controller_standard.go b/controllers/rest/controller_standard.go index 0ed96cb5..e64bcef7 100644 --- a/controllers/rest/controller_standard.go +++ b/controllers/rest/controller_standard.go @@ -684,8 +684,8 @@ func DefaultResponseMiddleware(api *RESTAPI, projection projections.Projection, } respCode, _ = strconv.Atoi(code) if resp.Value.Content != nil { - //check for if there is one mediatype or if h=there is no accept value - if len(resp.Value.Content) == 1 || mediaType == "" { + //check for if there is one mediatype or if there is no accept value or if the accept value is all + if len(resp.Value.Content) == 1 || mediaType == "" || strings.Replace(mediaType, "*", "", -1) == "/" { for key, content := range resp.Value.Content { if content.Example != nil { bytesArray, err = json.Marshal(content.Example) @@ -702,17 +702,23 @@ func DefaultResponseMiddleware(api *RESTAPI, projection projections.Projection, //check for if there are multiple mediatype } else if len(resp.Value.Content) > 1 { if resp.Value.Content[mediaType] == nil { + //check for wild card if strings.Contains(mediaType, "*") { mediaT := strings.Replace(mediaType, "*", "", -1) - if resp.Value.Content[mediaT] != nil && resp.Value.Content[mediaT].Example != nil { - bytesArray, err = json.Marshal(resp.Value.Content[mediaType].Example) - if err != nil { - api.e.Logger.Debugf("unexpected error %s ", err) - return NewControllerError(fmt.Sprintf("unexpected error %s ", err), err, http.StatusBadRequest) + for key, content := range resp.Value.Content { + if strings.Contains(key, mediaT) { + if content.Example != nil { + bytesArray, err = json.Marshal(content.Example) + if err != nil { + api.e.Logger.Debugf("unexpected error %s ", err) + return NewControllerError(fmt.Sprintf("unexpected error %s ", err), err, http.StatusBadRequest) + } + contentType = key + found = true + break + } } - contentType = mediaType - found = true - break + } } //search all the responses to find if the mediatype is there diff --git a/end2end_test.go b/end2end_test.go index 02b3f590..1bbf6aaa 100644 --- a/end2end_test.go +++ b/end2end_test.go @@ -1468,8 +1468,8 @@ func theContentTypeShouldBe(mediaType string) error { } func theHeaderIsSetWithValue(key, value string) error { - - return godog.ErrPending + header.Set(key, value) + return nil } func theResponseBodyShouldBe(expectResp *godog.DocString) error { @@ -1583,7 +1583,7 @@ func TestBDD(t *testing.T) { Options: &godog.Options{ Format: "pretty", //Tags: "~long && ~skipped", - Tags: "focus", + Tags: "WEOS-1365", //Tags: "WEOS-1110 && ~skipped", }, }.Run() diff --git a/features/default-response.feature b/features/default-response.feature index 1bbb781b..96de037a 100644 --- a/features/default-response.feature +++ b/features/default-response.feature @@ -65,7 +65,7 @@ Feature: Hardcode the response for an endpoint TestThis is a test page """ - @WEOS-1365 @focus + @WEOS-1365 Scenario: Send Accept header to hit at content of expected response If there are multiple content types you can send an [Accept header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept) From 0883b97e2890c33becb26674e0da1dfbbe5da3b2 Mon Sep 17 00:00:00 2001 From: shaniah868 Date: Fri, 25 Feb 2022 09:34:23 -0400 Subject: [PATCH 3/4] feature: WEOS-13065 Generate and Ensure Bdd Test is passing -Update the controller functionality --- controllers/rest/controller_standard.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controllers/rest/controller_standard.go b/controllers/rest/controller_standard.go index e64bcef7..e3a91b83 100644 --- a/controllers/rest/controller_standard.go +++ b/controllers/rest/controller_standard.go @@ -685,7 +685,7 @@ func DefaultResponseMiddleware(api *RESTAPI, projection projections.Projection, respCode, _ = strconv.Atoi(code) if resp.Value.Content != nil { //check for if there is one mediatype or if there is no accept value or if the accept value is all - if len(resp.Value.Content) == 1 || mediaType == "" || strings.Replace(mediaType, "*", "", -1) == "/" { + if len(resp.Value.Content) == 1 || mediaType == "" || strings.Replace(mediaType, "*", "", -1) == "/" || mediaType == "/" { for key, content := range resp.Value.Content { if content.Example != nil { bytesArray, err = json.Marshal(content.Example) From 018cd6ff9b709542465d7accaa5b9a499127685a Mon Sep 17 00:00:00 2001 From: shaniah868 Date: Fri, 25 Feb 2022 10:56:09 -0400 Subject: [PATCH 4/4] feature: WEOS-1365 Generate and Ensure Bdd Test is passing -putting tags back --- end2end_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/end2end_test.go b/end2end_test.go index 1bbf6aaa..8fdce33e 100644 --- a/end2end_test.go +++ b/end2end_test.go @@ -1582,8 +1582,8 @@ func TestBDD(t *testing.T) { TestSuiteInitializer: InitializeSuite, Options: &godog.Options{ Format: "pretty", - //Tags: "~long && ~skipped", - Tags: "WEOS-1365", + Tags: "~long && ~skipped", + //Tags: "WEOS-1365", //Tags: "WEOS-1110 && ~skipped", }, }.Run()