Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 2 additions & 17 deletions example/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ package example

import (
"encoding/json"
"math/rand"
)

const (
_IDChars string = "01234567890abcdef"
_DefaultIDlen int = 8
)

type User struct {
Expand All @@ -18,8 +12,8 @@ type User struct {

var (
Users []*User = []*User{
{ID: generateID(), Name: "eco", Age: 30},
{ID: generateID(), Name: "any", Age: 29},
{ID: "a4fb4201", Name: "eco", Age: 30},
{ID: "43bd1a0d", Name: "any", Age: 29},
}
)

Expand All @@ -44,12 +38,3 @@ func MarshalDiscardError(i interface{}) []byte {
enc, _ := json.Marshal(i)
return enc
}

func generateID() string {
idArr := make([]byte, _DefaultIDlen)
for i := range idArr {
index := rand.Intn(len(_IDChars))
idArr[i] = _IDChars[index]
}
return string(idArr)
}
27 changes: 19 additions & 8 deletions example/server/test_case.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package example

import (
"errors"
"net/http"

"github.com/ecoshub/taste/server"
)

var (
TestUser *User = &User{ID: "718c9a02", Name: "john", Age: 20}
TestUser []byte = []byte(`{"id":"718c9a02","name":"john","age":20}`)
)

var (
Expand All @@ -19,8 +20,8 @@ var (
Path: "/api/v1/version",
},
Expect: &server.Expect{
Status: http.StatusOK,
BodyString: "v1.0.0",
Status: http.StatusOK,
Body: []byte("v1.0.0"),
},
},
{
Expand All @@ -31,7 +32,12 @@ var (
},
Expect: &server.Expect{
Status: http.StatusOK,
Body: MarshalDiscardError(Users),
Body: []byte(`
[
{"id":"a4fb4201","name":"eco","age":30},
{"id":"43bd1a0d","name":"any","age":29}
]`),
Error: errors.New("type expectation failed. expected type: 'int', got type: 'string', path: '[0 id]'"),
},
},
{
Expand All @@ -53,7 +59,7 @@ var (
},
Expect: &server.Expect{
Status: http.StatusOK,
Body: MarshalDiscardError(Users[0]),
Body: []byte(`{"id":"a4fb4201","name":"eco","age":30}`),
},
},
{
Expand All @@ -64,7 +70,7 @@ var (
},
Expect: &server.Expect{
Status: http.StatusOK,
Body: MarshalDiscardError(Users[1]),
Body: []byte(`{"id":"43bd1a0d","name":"any","age":29}`),
Header: http.Header{
"Content-Type": []string{"application/json; charset=utf-8"},
},
Expand All @@ -75,11 +81,16 @@ var (
Request: &server.Request{
Method: http.MethodPost,
Path: "/api/v1/user/new",
Body: MarshalDiscardError(TestUser),
Body: []byte(`{"id":"718c9a02","name":"john","age":20}`),
},
Expect: &server.Expect{
Status: http.StatusOK,
Body: MarshalDiscardError(append(Users, TestUser)),
Body: []byte(`
[
{"id":"a4fb4201","name":"eco","age":30},
{"id":"43bd1a0d","name":"any","age":29},
{"id":"718c9a02","name":"john","age":20}
]`),
Header: http.Header{
"Content-Type": []string{"application/json; charset=utf-8"},
},
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/ecoshub/taste

go 1.16

require github.com/gin-gonic/gin v1.8.1
require (
github.com/ecoshub/jin v0.0.0-20220630143555-793085706848
github.com/gin-gonic/gin v1.8.1
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/ecoshub/jin v0.0.0-20220630143555-793085706848 h1:3bYBxwUprgL61kqd+L2mg+IDZRkockHBn5f66nqYlw8=
github.com/ecoshub/jin v0.0.0-20220630143555-793085706848/go.mod h1:M5bdVepcqwxZZ6tCE0HQxTytn6DYYnzFL/zPKKwMW+4=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.8.1 h1:4+fr/el88TOO3ewCmQr8cx/CtZ/umlIRIs5M4NTNjf8=
Expand Down
1 change: 1 addition & 0 deletions server/case.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ type Expect struct {
Body []byte
BodyString string
Header http.Header
Error error
}
15 changes: 10 additions & 5 deletions server/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,18 @@ func run(sc *Tester, c *Case, t *testing.T) {
}
defer resp.Body.Close()

if c.Expect.Body == nil {
if c.Expect.BodyString != "" {
utils.CheckEqual(t, "response-body", string(body), c.Expect.BodyString)
err = utils.Validate(c.Expect.Body, body)
// got error
if err != nil {
// expecting error
if c.Expect.Error != nil {
utils.CheckEqual(t, "error", err, c.Expect.Error)
return
}
} else {
utils.CheckEqual(t, "response-body", string(body), string(c.Expect.Body))
// utils.Fail(t, "", c.Expect.Body, body)
t.Fatalf("err: %v. expected: %s, got: %s", err, c.Expect.Body, body)
}

}

func resolveBody(r *Request) *bytes.Buffer {
Expand Down
Loading