Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-151: Added HTTP HEAD method request support.
- Loading branch information
Showing
2 changed files
with
96 additions
and
19 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,71 @@ | ||
package test | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/resgateio/resgate/server/reserr" | ||
) | ||
|
||
// Test invalid urls for HTTP get requests | ||
func TestHTTPMethodHEAD_InvalidURLs_CorrectStatus(t *testing.T) { | ||
tbl := []struct { | ||
URL string // Url path | ||
ExpectedCode int | ||
}{ | ||
{"/wrong_prefix/test/model", http.StatusNotFound}, | ||
{"/api/", http.StatusNotFound}, | ||
{"/api/test.model", http.StatusNotFound}, | ||
{"/api/test/model/", http.StatusNotFound}, | ||
{"/api/test//model", http.StatusNotFound}, | ||
{"/api/test/mådel/action", http.StatusNotFound}, | ||
} | ||
|
||
for i, l := range tbl { | ||
runNamedTest(t, fmt.Sprintf("#%d", i+1), func(s *Session) { | ||
s.HTTPRequest("HEAD", l.URL, nil). | ||
GetResponse(t). | ||
AssertStatusCode(t, l.ExpectedCode). | ||
AssertBody(t, nil) | ||
}) | ||
} | ||
} | ||
|
||
func TestHTTPHead_OnSuccess_NoBody(t *testing.T) { | ||
model := resourceData("test.model") | ||
runTest(t, func(s *Session) { | ||
hreq := s.HTTPRequest("HEAD", "/api/test/model", nil) | ||
|
||
/// Handle model get and access request | ||
mreqs := s.GetParallelRequests(t, 2) | ||
req := mreqs.GetRequest(t, "access.test.model") | ||
req.RespondSuccess(json.RawMessage(`{"get":true}`)) | ||
req = mreqs.GetRequest(t, "get.test.model") | ||
req.RespondSuccess(json.RawMessage(`{"model":` + model + `}`)) | ||
|
||
// Validate http response | ||
hreq.GetResponse(t). | ||
AssertStatusCode(t, http.StatusOK). | ||
AssertBody(t, nil) | ||
}) | ||
} | ||
|
||
func TestHTTPHead_OnError_NoBody(t *testing.T) { | ||
runTest(t, func(s *Session) { | ||
hreq := s.HTTPRequest("HEAD", "/api/test/model", nil) | ||
|
||
/// Handle model get and access request | ||
mreqs := s.GetParallelRequests(t, 2) | ||
req := mreqs.GetRequest(t, "access.test.model") | ||
req.RespondSuccess(json.RawMessage(`{"get":true}`)) | ||
req = mreqs.GetRequest(t, "get.test.model") | ||
req.RespondError(reserr.ErrNotFound) | ||
|
||
// Validate http response | ||
hreq.GetResponse(t). | ||
AssertStatusCode(t, http.StatusNotFound). | ||
AssertBody(t, nil) | ||
}) | ||
} |