-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
lixizan
committed
May 23, 2022
1 parent
b8197df
commit ac30dee
Showing
7 changed files
with
81 additions
and
28 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,65 @@ | ||
package hasaki | ||
|
||
import "testing" | ||
import ( | ||
"bytes" | ||
"io" | ||
"net/http" | ||
"testing" | ||
|
||
jsoniter "github.com/json-iterator/go" | ||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type responseBody struct { | ||
Code int `json:"code"` | ||
Message string `json:"message"` | ||
} | ||
|
||
func (this responseBody) Error() string { | ||
return this.Message | ||
} | ||
|
||
func TestNewRequest(t *testing.T) { | ||
const baseurl = "http://localhost:9000" | ||
if _, err := Get(baseurl + "/p1").Send(nil).GetBody(); err != nil { | ||
t.Error(err.Error()) | ||
} | ||
|
||
if _, err := Get(baseurl + "/p2").Send(nil).GetBody(); err == nil { | ||
t.Error(err.Error()) | ||
} | ||
var checker ErrorChecker = func(resp *http.Response) error { | ||
if resp.StatusCode != http.StatusOK { | ||
return errors.New("StatusCodeError") | ||
} | ||
|
||
if _, err := Get(baseurl + "/p3").Send(nil).GetBody(); err == nil { | ||
t.Error(err.Error()) | ||
} | ||
var buf = bytes.NewBuffer(nil) | ||
if _, err := io.Copy(buf, resp.Body); err != nil { | ||
return err | ||
} | ||
resp.Body.Close() | ||
var body = &responseBody{} | ||
jsoniter.Unmarshal(buf.Bytes(), body) | ||
if body.Code != http.StatusOK { | ||
return body | ||
} | ||
|
||
if _, err := Get(baseurl + "/p4").Send(nil).GetBody(); err == nil { | ||
t.Error(err.Error()) | ||
resp.Body = io.NopCloser(buf) | ||
return nil | ||
} | ||
|
||
var as = assert.New(t) | ||
|
||
t.Run("StatusOK", func(t *testing.T) { | ||
body, err := Post(baseurl + "/200"). | ||
SetErrorChecker(checker). | ||
Send(nil). | ||
GetBody() | ||
as.NoError(err) | ||
t.Logf("body: %s", string(body)) | ||
}) | ||
|
||
t.Run("StatusBadRequest", func(t *testing.T) { | ||
_, err := Post(baseurl + "/400"). | ||
SetErrorChecker(checker). | ||
Send(nil). | ||
GetBody() | ||
as.Error(err) | ||
t.Logf("err: %v", err) | ||
}) | ||
} |
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