fix: mock server fails to handle form encoded POST request payload#165
fix: mock server fails to handle form encoded POST request payload#165mtronix wants to merge 1 commit intopb33f:mainfrom
Conversation
| var bodyJsonObj interface{} | ||
| err = json.Unmarshal(bodyBytes, &bodyJsonObj) | ||
| if err != nil { | ||
| sms.logger.Error("Error decoding JSON of incoming request. JSON => \n%s", string(bodyBytes), err) |
There was a problem hiding this comment.
This changes the current logic of throwing an error when the json body is of wrong type. Why are we removing this check and accepting string here?
Ideally if the content-type is json then the body being string should throw a 400.
| func (sms *StaticMockService) compareStructuredBody(mock StaticMockDefinitionRequest, request *http.Request) bool { | ||
| contentType := request.Header.Get("Content-Type") | ||
| // Only support JSON and form-urlencoded for structured body comparison | ||
| if contentType != "application/json" && contentType != "application/x-www-form-urlencoded" { |
There was a problem hiding this comment.
Instead of handling it here, we should create a separate path for handling application/x-www-form-urlencoded type content. That way the json flow stays untouched and we just add a new code path for handing this type of request.
| ) | ||
|
|
||
| // getBodyFromHttpRequest reads the body of the incoming request and returns it as an interface{} | ||
| func (sms *StaticMockService) getBodyFromHttpRequest(request *http.Request) interface{} { |
There was a problem hiding this comment.
Building on https://github.com/pb33f/wiretap/pull/165/changes#r2794736837
There should be a switch here and it should leave the json type flow untouched and add a new method for processing application/x-www-form-urlencoded type body.
Fix for the issue:
#164