-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More gracefully handle inputs that exceed the limit
- Loading branch information
Showing
2 changed files
with
59 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package server | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestGetRequestFromHTTPRequest(t *testing.T) { | ||
|
||
t.Run("empty payload", func(t *testing.T) { | ||
request, err := createStubbedTySugRequest(strings.NewReader("{}")) | ||
if err != nil { | ||
t.Errorf("Expected error was thrown '%s'\n%+v", err, request) | ||
} | ||
}) | ||
|
||
t.Run("payload too large", func(t *testing.T) { | ||
i := strings.Repeat("a", maxBodySize+1) | ||
payload, _ := json.Marshal(tySugRequest{Input: i}) | ||
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(payload)) | ||
|
||
request, err := getRequestFromHTTPRequest(req) | ||
if err != ErrBodyTooLarge { | ||
t.Errorf("Expected an error to be thrown %+v", request) | ||
} | ||
}) | ||
|
||
t.Run("invalid request body", func(t *testing.T) { | ||
request, err := createStubbedTySugRequest(strings.NewReader("{Input: nil}")) | ||
if err != ErrInvalidRequestBody { | ||
t.Errorf("Expected an error to be thrown %+v", request) | ||
} | ||
}) | ||
} | ||
|
||
func createStubbedTySugRequest(r io.Reader) (tySugRequest, error) { | ||
req := httptest.NewRequest(http.MethodPost, "/", r) | ||
return getRequestFromHTTPRequest(req) | ||
} |