Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: WEOS-1365 Generate and Ensure Bdd Test is passing #123

Merged
merged 4 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 54 additions & 26 deletions controllers/rest/controller_standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,55 +671,83 @@ 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 there is no accept value or if the accept value is all
if len(resp.Value.Content) == 1 || mediaType == "" || strings.Replace(mediaType, "*", "", -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)
//check for wild card
if strings.Contains(mediaType, "*") {
mediaT := strings.Replace(mediaType, "*", "", -1)
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
}
}

}
}
//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)

}
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/rest/controller_standard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
Expand Down
35 changes: 34 additions & 1 deletion end2end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -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 {
header.Set(key, value)
return nil
}

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) {
Expand Down Expand Up @@ -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)

}

Expand All @@ -1550,7 +1583,7 @@ func TestBDD(t *testing.T) {
Options: &godog.Options{
Format: "pretty",
Tags: "~long && ~skipped",
//Tags: "focus1",
//Tags: "WEOS-1365",
//Tags: "WEOS-1110 && ~skipped",
},
}.Run()
Expand Down
4 changes: 4 additions & 0 deletions features/default-response.feature
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Feature: Hardcode the response for an endpoint
example: |
<html> <head><title>Test</title></head><body>This is a test page</body></html>
"""
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"
Expand Down Expand Up @@ -55,12 +56,14 @@ Feature: Hardcode the response for an endpoint
example: |
<html> <head><title>Page Not Found</title></head><body>Some not found page</body></html>
"""
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
"""
<html> <head><title>Test</title></head><body>This is a test page</body></html>

"""
@WEOS-1365
Scenario: Send Accept header to hit at content of expected response
Expand All @@ -84,6 +87,7 @@ Feature: Hardcode the response for an endpoint
example: |
<page><title>Test</title></page>
"""
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
Expand Down