Skip to content

Commit

Permalink
api test
Browse files Browse the repository at this point in the history
  • Loading branch information
lixizan committed May 23, 2022
1 parent b8197df commit ac30dee
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 28 deletions.
5 changes: 3 additions & 2 deletions encoding.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package hasaki

import (
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
"net/url"
"reflect"

jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
)

type Any map[string]interface{}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ go 1.16
require (
github.com/json-iterator/go v1.1.10
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.3.0
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
3 changes: 2 additions & 1 deletion request.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package hasaki
import (
"bytes"
"fmt"
"github.com/pkg/errors"
"io"
"net/http"
neturl "net/url"
"strings"
"time"

"github.com/pkg/errors"
)

var DefaultClient = &http.Client{
Expand Down
67 changes: 55 additions & 12 deletions request_test.go
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)
})
}
5 changes: 3 additions & 2 deletions response.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package hasaki

import (
"github.com/json-iterator/go"
"github.com/pkg/errors"
"io/ioutil"
"net/http"

jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
)

type ErrorChecker func(resp *http.Response) error
Expand Down
27 changes: 16 additions & 11 deletions server/test_server.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package main

import (
jsoniter "github.com/json-iterator/go"
"net/http"

jsoniter "github.com/json-iterator/go"
)

var response = map[string]interface{}{
"success": true,
type ResponseBody struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
}

func WriteJson(writer http.ResponseWriter, code int, v interface{}) error {
Expand All @@ -21,16 +24,18 @@ func WriteJson(writer http.ResponseWriter, code int, v interface{}) error {
}

func main() {
http.HandleFunc("/p1", func(writer http.ResponseWriter, request *http.Request) {
WriteJson(writer, 200, response)
})

http.HandleFunc("/p2", func(writer http.ResponseWriter, request *http.Request) {
WriteJson(writer, 400, response)
http.HandleFunc("/200", func(writer http.ResponseWriter, request *http.Request) {
WriteJson(writer, http.StatusOK, ResponseBody{
Code: http.StatusOK,
Message: "success",
})
})

http.HandleFunc("/p4", func(writer http.ResponseWriter, request *http.Request) {
WriteJson(writer, 500, response)
http.HandleFunc("/400", func(writer http.ResponseWriter, request *http.Request) {
WriteJson(writer, http.StatusOK, ResponseBody{
Code: http.StatusBadRequest,
Message: "StatusBadRequest",
})
})

http.ListenAndServe(":9000", nil)
Expand Down

0 comments on commit ac30dee

Please sign in to comment.