test: add transport http test (#1579)
* test: add transport http test * fix lient * add client * add http testpull/1582/head
parent
91714bcd34
commit
3da927759a
@ -0,0 +1,94 @@ |
||||
package binding |
||||
|
||||
import ( |
||||
"net/http" |
||||
"net/url" |
||||
"reflect" |
||||
"testing" |
||||
) |
||||
|
||||
func TestBindQuery(t *testing.T) { |
||||
type TestBind struct { |
||||
Name string `json:"name"` |
||||
URL string `json:"url"` |
||||
} |
||||
p1 := TestBind{} |
||||
type args struct { |
||||
vars url.Values |
||||
target interface{} |
||||
} |
||||
tests := []struct { |
||||
name string |
||||
args args |
||||
wantErr bool |
||||
want interface{} |
||||
}{ |
||||
{ |
||||
name: "test", |
||||
args: args{ |
||||
vars: map[string][]string{"name": {"kratos"}, "url": {"https://go-kratos.dev/"}}, |
||||
target: &p1, |
||||
}, |
||||
wantErr: false, |
||||
want: TestBind{"kratos", "https://go-kratos.dev/"}, |
||||
}, |
||||
} |
||||
for _, tt := range tests { |
||||
t.Run(tt.name, func(t *testing.T) { |
||||
if err := BindQuery(tt.args.vars, tt.args.target); (err != nil) != tt.wantErr { |
||||
t.Errorf("BindQuery() error = %v, wantErr %v", err, tt.wantErr) |
||||
} |
||||
if reflect.DeepEqual(tt.args.target, tt.want) { |
||||
t.Errorf("BindQuery() target = %v, want %v", tt.args.target, tt.want) |
||||
} |
||||
}) |
||||
} |
||||
} |
||||
|
||||
func TestBindForm(t *testing.T) { |
||||
type TestBind struct { |
||||
Name string `json:"name"` |
||||
URL string `json:"url"` |
||||
} |
||||
p1 := TestBind{} |
||||
type args struct { |
||||
req *http.Request |
||||
target interface{} |
||||
} |
||||
tests := []struct { |
||||
name string |
||||
args args |
||||
wantErr bool |
||||
want interface{} |
||||
}{ |
||||
{ |
||||
name: "error not nil", |
||||
args: args{ |
||||
req: &http.Request{Method: "POST"}, |
||||
target: &p1, |
||||
}, |
||||
wantErr: true, |
||||
want: nil, |
||||
}, |
||||
{ |
||||
name: "error is nil", |
||||
args: args{ |
||||
req: &http.Request{Form: map[string][]string{"name": {"kratos"}, "url": {"https://go-kratos.dev/"}}}, |
||||
target: &p1, |
||||
}, |
||||
wantErr: false, |
||||
want: TestBind{"kratos", "https://go-kratos.dev/"}, |
||||
}, |
||||
} |
||||
for _, tt := range tests { |
||||
t.Run(tt.name, func(t *testing.T) { |
||||
err := BindForm(tt.args.req, tt.args.target) |
||||
if (err != nil) != tt.wantErr { |
||||
t.Errorf("BindForm() error = %v, wantErr %v", err, tt.wantErr) |
||||
} |
||||
if err != nil && reflect.DeepEqual(tt.args.target, tt.want) { |
||||
t.Errorf("BindQuery() target = %v, want %v", tt.args.target, tt.want) |
||||
} |
||||
}) |
||||
} |
||||
} |
@ -1 +1,168 @@ |
||||
package http |
||||
|
||||
import ( |
||||
"bytes" |
||||
"context" |
||||
"net/http" |
||||
"net/http/httptest" |
||||
"net/url" |
||||
"testing" |
||||
"time" |
||||
|
||||
"github.com/stretchr/testify/assert" |
||||
) |
||||
|
||||
func TestContextHeader(t *testing.T) { |
||||
w := wrapper{ |
||||
router: nil, |
||||
req: &http.Request{Header: map[string][]string{"name": {"kratos"}}}, |
||||
res: nil, |
||||
w: responseWriter{}, |
||||
} |
||||
h := w.Header() |
||||
assert.Equal(t, h, http.Header{"name": {"kratos"}}) |
||||
} |
||||
|
||||
func TestContextForm(t *testing.T) { |
||||
w := wrapper{ |
||||
router: nil, |
||||
req: &http.Request{Header: map[string][]string{"name": {"kratos"}}, Method: "POST"}, |
||||
res: nil, |
||||
w: responseWriter{}, |
||||
} |
||||
form := w.Form() |
||||
assert.Equal(t, form, url.Values{}) |
||||
|
||||
w = wrapper{ |
||||
router: nil, |
||||
req: &http.Request{Form: map[string][]string{"name": {"kratos"}}}, |
||||
res: nil, |
||||
w: responseWriter{}, |
||||
} |
||||
form = w.Form() |
||||
assert.Equal(t, form, url.Values{"name": []string{"kratos"}}) |
||||
} |
||||
|
||||
func TestContextQuery(t *testing.T) { |
||||
w := wrapper{ |
||||
router: nil, |
||||
req: &http.Request{URL: &url.URL{Scheme: "https", Host: "github.com", Path: "go-kratos/kratos", RawQuery: "page=1"}, Method: "POST"}, |
||||
res: nil, |
||||
w: responseWriter{}, |
||||
} |
||||
q := w.Query() |
||||
assert.Equal(t, q, url.Values{"page": []string{"1"}}) |
||||
} |
||||
|
||||
func TestContextRequest(t *testing.T) { |
||||
req := &http.Request{Method: "POST"} |
||||
w := wrapper{ |
||||
router: nil, |
||||
req: req, |
||||
res: nil, |
||||
w: responseWriter{}, |
||||
} |
||||
res := w.Request() |
||||
assert.Equal(t, res, req) |
||||
} |
||||
|
||||
func TestContextResponse(t *testing.T) { |
||||
res := httptest.NewRecorder() |
||||
w := wrapper{ |
||||
router: &Router{srv: &Server{enc: DefaultResponseEncoder}}, |
||||
req: &http.Request{Method: "POST"}, |
||||
res: res, |
||||
w: responseWriter{200, res}, |
||||
} |
||||
assert.Equal(t, w.Response(), res) |
||||
err := w.Returns(map[string]string{}, nil) |
||||
assert.Nil(t, err) |
||||
} |
||||
|
||||
func TestContextBindQuery(t *testing.T) { |
||||
w := wrapper{ |
||||
router: nil, |
||||
req: &http.Request{URL: &url.URL{Scheme: "https", Host: "go-kratos-dev", RawQuery: "page=2"}}, |
||||
res: nil, |
||||
w: responseWriter{}, |
||||
} |
||||
type BindQuery struct { |
||||
Page int `json:"page"` |
||||
} |
||||
b := BindQuery{} |
||||
err := w.BindQuery(&b) |
||||
assert.Nil(t, err) |
||||
assert.Equal(t, b, BindQuery{Page: 2}) |
||||
} |
||||
|
||||
func TestContextBindForm(t *testing.T) { |
||||
w := wrapper{ |
||||
router: nil, |
||||
req: &http.Request{URL: &url.URL{Scheme: "https", Host: "go-kratos-dev"}, Form: map[string][]string{"page": {"2"}}}, |
||||
res: nil, |
||||
w: responseWriter{}, |
||||
} |
||||
type BindForm struct { |
||||
Page int `json:"page"` |
||||
} |
||||
b := BindForm{} |
||||
err := w.BindForm(&b) |
||||
assert.Nil(t, err) |
||||
assert.Equal(t, b, BindForm{Page: 2}) |
||||
} |
||||
|
||||
func TestContextResponseReturn(t *testing.T) { |
||||
writer := httptest.NewRecorder() |
||||
w := wrapper{ |
||||
router: nil, |
||||
req: nil, |
||||
res: writer, |
||||
w: responseWriter{}, |
||||
} |
||||
err := w.JSON(200, "success") |
||||
assert.Nil(t, err) |
||||
err = w.XML(200, "success") |
||||
assert.Nil(t, err) |
||||
err = w.String(200, "success") |
||||
assert.Nil(t, err) |
||||
err = w.Blob(200, "blob", []byte("success")) |
||||
assert.Nil(t, err) |
||||
err = w.Stream(200, "stream", bytes.NewBuffer([]byte("success"))) |
||||
assert.Nil(t, err) |
||||
} |
||||
|
||||
func TestContextCtx(t *testing.T) { |
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
||||
defer cancel() |
||||
req := &http.Request{Method: "POST"} |
||||
req = req.WithContext(ctx) |
||||
w := wrapper{ |
||||
router: &Router{srv: &Server{enc: DefaultResponseEncoder}}, |
||||
req: req, |
||||
res: nil, |
||||
w: responseWriter{}, |
||||
} |
||||
_, ok := w.Deadline() |
||||
assert.Equal(t, ok, true) |
||||
done := w.Done() |
||||
assert.NotNil(t, done) |
||||
err := w.Err() |
||||
assert.Nil(t, err) |
||||
v := w.Value("test") |
||||
assert.Nil(t, v) |
||||
|
||||
w = wrapper{ |
||||
router: &Router{srv: &Server{enc: DefaultResponseEncoder}}, |
||||
req: nil, |
||||
res: nil, |
||||
w: responseWriter{}, |
||||
} |
||||
_, ok = w.Deadline() |
||||
assert.Equal(t, ok, false) |
||||
done = w.Done() |
||||
assert.Nil(t, done) |
||||
err = w.Err() |
||||
assert.NotNil(t, err) |
||||
v = w.Value("test") |
||||
assert.Nil(t, v) |
||||
} |
||||
|
Loading…
Reference in new issue