-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from bozd4g/bug/context-canceling-problem
Context canceling problem
- Loading branch information
Showing
7 changed files
with
178 additions
and
51 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,74 @@ | ||
package gohttpclient | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type IntegrationSuite struct { | ||
suite.Suite | ||
bigJsonFile []byte | ||
} | ||
|
||
func TestIntegrationSuite(t *testing.T) { | ||
suite.Run(t, new(IntegrationSuite)) | ||
} | ||
|
||
func (s *IntegrationSuite) SetupSuite() {} | ||
|
||
func (s *IntegrationSuite) Test_Get_WhenServerReturnsBigFile_ShouldRunSuccesfully() { | ||
// Arrange | ||
type Post struct { | ||
ID int `json:"id"` | ||
Title string `json:"title"` | ||
} | ||
|
||
ctx := context.Background() | ||
|
||
// read large file from test folder | ||
testSvc := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
bigArr := make([]Post, 0) | ||
for i := 0; i < 100000; i++ { | ||
bigArr = append(bigArr, Post{ID: i, Title: fmt.Sprintf("Title %d", i)}) | ||
} | ||
|
||
postJson, err := json.Marshal(bigArr) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
// write cookie | ||
http.SetCookie(w, &http.Cookie{Name: "test", Value: "test"}) | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
w.Write(postJson) | ||
})) | ||
|
||
client := New(testSvc.URL) | ||
|
||
// Act | ||
resp, err := client.Get(ctx, "/") | ||
body := resp.Body() | ||
status := resp.Status() | ||
headers := resp.Headers() | ||
cookies := resp.Cookies() | ||
ok := resp.Ok() | ||
res := resp.Get() | ||
|
||
// Assert | ||
s.NoError(err) | ||
s.NotNil(resp) | ||
s.NotNil(body) | ||
s.Equal(http.StatusOK, status) | ||
s.Equal("application/json", headers.Get("Content-Type")) | ||
s.Equal(1, len(cookies)) | ||
s.True(ok) | ||
s.NotNil(res) | ||
} |
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
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
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