This repository was archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 98
Make sure event body is a string #393
Merged
Merged
Changes from 12 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f12f965
Add form to string conversion in eventFromRequest
RaeesBhatti 5969a23
Reformat logic
RaeesBhatti 56cffd8
Change a func name
RaeesBhatti b4d20c7
Update the switch case logic
RaeesBhatti 74512a1
Remove the function
RaeesBhatti dbedb9e
Add comment to explain the process
RaeesBhatti 1a4d8ab
Update comment
RaeesBhatti 4ef70a4
Merge branch 'master' into fixEncoding
RaeesBhatti 1e761d7
Add Provider mock
RaeesBhatti fcbbd29
Add encoding test
RaeesBhatti 7cce702
Add missing break
RaeesBhatti 83f57ae
Fix the test
RaeesBhatti 841fb1d
Remove breaks
RaeesBhatti 1c37d3d
Rename a test func
RaeesBhatti 14dae4e
Add multiple scenarios in the test
RaeesBhatti 6b11d76
Change a key name
RaeesBhatti 9e63be3
Add test case for multipart/form-data
RaeesBhatti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,8 @@ type HTTPResponse struct { | |
|
||
const ( | ||
mimeJSON = "application/json" | ||
mimeFormMultipart = "multipart/form-data" | ||
mimeFormURLEncoded = "application/x-www-form-urlencoded" | ||
) | ||
|
||
func isHTTPEvent(r *http.Request) bool { | ||
|
@@ -63,10 +65,20 @@ func (router *Router) eventFromRequest(r *http.Request) (*eventpkg.Event, string | |
} | ||
|
||
event := eventpkg.New(eventType, mime, body) | ||
if mime == mimeJSON && len(body) > 0 { | ||
err = json.Unmarshal(body, &event.Data) | ||
if err != nil { | ||
return nil, "", errors.New("malformed JSON body") | ||
|
||
// Because event.Data is []bytes here, it will be base64 encoded by default when being sent to remote function, | ||
// which is why we change the event.Data type to "string" for forms, so that, it is left intact. | ||
if len(body) > 0 { | ||
switch { | ||
case mime == mimeJSON: | ||
err := json.Unmarshal(body, &event.Data) | ||
if err != nil { | ||
return nil, "", errors.New("malformed JSON body") | ||
} | ||
break | ||
case strings.HasPrefix(mime, mimeFormMultipart), mime == mimeFormURLEncoded: | ||
event.Data = string(body) | ||
break | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @elsteelbrain Same as above |
||
} | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
//go:generate mockgen -package mock -destination ./targetcache.go github.com/serverless/event-gateway/router Targeter | ||
//go:generate mockgen -package mock -destination ./provider.go github.com/serverless/event-gateway/function Provider | ||
|
||
package mock |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@elsteelbrain
break
isn't needed in Golang case statements -- https://tour.golang.org/flowcontrol/9