Merge pull request #4387 from flycash/http-mock

Support httplib mock
This commit is contained in:
Ming Deng 2020-12-25 22:21:28 +08:00 committed by GitHub
commit b0406f107a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 483 additions and 3 deletions

View File

@ -0,0 +1,173 @@
// Copyright 2020 beego
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mock
import (
"context"
"encoding/json"
"net/textproto"
"regexp"
"github.com/beego/beego/v2/client/httplib"
)
// reqCondition create condition
// - path: same path
// - pathReg: request path match pathReg
// - method: same method
// - Query parameters (key, value)
// - header (key, value)
// - Body json format, contains specific (key, value).
type SimpleCondition struct {
pathReg string
path string
method string
query map[string]string
header map[string]string
body map[string]interface{}
}
func NewSimpleCondition(path string, opts ...simpleConditionOption) *SimpleCondition {
sc := &SimpleCondition{
path: path,
query: make(map[string]string),
header: make(map[string]string),
body: map[string]interface{}{},
}
for _, o := range opts {
o(sc)
}
return sc
}
func (sc *SimpleCondition) Match(ctx context.Context, req *httplib.BeegoHTTPRequest) bool {
res := true
if len(sc.path) > 0 {
res = sc.matchPath(ctx, req)
} else if len(sc.pathReg) > 0 {
res = sc.matchPathReg(ctx, req)
} else {
return false
}
return res &&
sc.matchMethod(ctx, req) &&
sc.matchQuery(ctx, req) &&
sc.matchHeader(ctx, req) &&
sc.matchBodyFields(ctx, req)
}
func (sc *SimpleCondition) matchPath(ctx context.Context, req *httplib.BeegoHTTPRequest) bool {
path := req.GetRequest().URL.Path
return path == sc.path
}
func (sc *SimpleCondition) matchPathReg(ctx context.Context, req *httplib.BeegoHTTPRequest) bool {
path := req.GetRequest().URL.Path
if b, err := regexp.Match(sc.pathReg, []byte(path)); err == nil {
return b
}
return false
}
func (sc *SimpleCondition) matchQuery(ctx context.Context, req *httplib.BeegoHTTPRequest) bool {
qs := req.GetRequest().URL.Query()
for k, v := range sc.query {
if uv, ok := qs[k]; !ok || uv[0] != v {
return false
}
}
return true
}
func (sc *SimpleCondition) matchHeader(ctx context.Context, req *httplib.BeegoHTTPRequest) bool {
headers := req.GetRequest().Header
for k, v := range sc.header {
if uv, ok := headers[k]; !ok || uv[0] != v {
return false
}
}
return true
}
func (sc *SimpleCondition) matchBodyFields(ctx context.Context, req *httplib.BeegoHTTPRequest) bool {
if len(sc.body) == 0 {
return true
}
getBody := req.GetRequest().GetBody
body, err := getBody()
if err != nil {
return false
}
bytes := make([]byte, req.GetRequest().ContentLength)
_, err = body.Read(bytes)
if err != nil {
return false
}
m := make(map[string]interface{})
err = json.Unmarshal(bytes, &m)
if err != nil {
return false
}
for k, v := range sc.body {
if uv, ok := m[k]; !ok || uv != v {
return false
}
}
return true
}
func (sc *SimpleCondition) matchMethod(ctx context.Context, req *httplib.BeegoHTTPRequest) bool {
if len(sc.method) > 0 {
return sc.method == req.GetRequest().Method
}
return true
}
type simpleConditionOption func(sc *SimpleCondition)
func WithPathReg(pathReg string) simpleConditionOption {
return func(sc *SimpleCondition) {
sc.pathReg = pathReg
}
}
func WithQuery(key, value string) simpleConditionOption {
return func(sc *SimpleCondition) {
sc.query[key] = value
}
}
func WithHeader(key, value string) simpleConditionOption {
return func(sc *SimpleCondition) {
sc.header[textproto.CanonicalMIMEHeaderKey(key)] = value
}
}
func WithJsonBodyFields(field string, value interface{}) simpleConditionOption {
return func(sc *SimpleCondition) {
sc.body[field] = value
}
}
func WithMethod(method string) simpleConditionOption {
return func(sc *SimpleCondition) {
sc.method = method
}
}

View File

@ -0,0 +1,124 @@
// Copyright 2020 beego
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mock
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/beego/beego/v2/client/httplib"
)
func init() {
}
func TestSimpleCondition_MatchPath(t *testing.T) {
sc := NewSimpleCondition("/abc/s")
res := sc.Match(context.Background(), httplib.Get("http://localhost:8080/abc/s"))
assert.True(t, res)
}
func TestSimpleCondition_MatchQuery(t *testing.T) {
k, v := "my-key", "my-value"
sc := NewSimpleCondition("/abc/s")
res := sc.Match(context.Background(), httplib.Get("http://localhost:8080/abc/s?my-key=my-value"))
assert.True(t, res)
sc = NewSimpleCondition("/abc/s", WithQuery(k, v))
res = sc.Match(context.Background(), httplib.Get("http://localhost:8080/abc/s?my-key=my-value"))
assert.True(t, res)
res = sc.Match(context.Background(), httplib.Get("http://localhost:8080/abc/s?my-key=my-valuesss"))
assert.False(t, res)
res = sc.Match(context.Background(), httplib.Get("http://localhost:8080/abc/s?my-key-a=my-value"))
assert.False(t, res)
res = sc.Match(context.Background(), httplib.Get("http://localhost:8080/abc/s?my-key=my-value&abc=hello"))
assert.True(t, res)
}
func TestSimpleCondition_MatchHeader(t *testing.T) {
k, v := "my-header", "my-header-value"
sc := NewSimpleCondition("/abc/s")
req := httplib.Get("http://localhost:8080/abc/s")
assert.True(t, sc.Match(context.Background(), req))
req = httplib.Get("http://localhost:8080/abc/s")
req.Header(k, v)
assert.True(t, sc.Match(context.Background(), req))
sc = NewSimpleCondition("/abc/s", WithHeader(k, v))
req.Header(k, v)
assert.True(t, sc.Match(context.Background(), req))
req.Header(k, "invalid")
assert.False(t, sc.Match(context.Background(), req))
}
func TestSimpleCondition_MatchBodyField(t *testing.T) {
sc := NewSimpleCondition("/abc/s")
req := httplib.Post("http://localhost:8080/abc/s")
assert.True(t, sc.Match(context.Background(), req))
req.Body(`{
"body-field": 123
}`)
assert.True(t, sc.Match(context.Background(), req))
k := "body-field"
v := float64(123)
sc = NewSimpleCondition("/abc/s", WithJsonBodyFields(k, v))
assert.True(t, sc.Match(context.Background(), req))
sc = NewSimpleCondition("/abc/s", WithJsonBodyFields(k, v))
req.Body(`{
"body-field": abc
}`)
assert.False(t, sc.Match(context.Background(), req))
sc = NewSimpleCondition("/abc/s", WithJsonBodyFields("body-field", "abc"))
req.Body(`{
"body-field": "abc"
}`)
assert.True(t, sc.Match(context.Background(), req))
}
func TestSimpleCondition_Match(t *testing.T) {
sc := NewSimpleCondition("/abc/s")
req := httplib.Post("http://localhost:8080/abc/s")
assert.True(t, sc.Match(context.Background(), req))
sc = NewSimpleCondition("/abc/s", WithMethod("POST"))
assert.True(t, sc.Match(context.Background(), req))
sc = NewSimpleCondition("/abc/s", WithMethod("GET"))
assert.False(t, sc.Match(context.Background(), req))
}
func TestSimpleCondition_MatchPathReg(t *testing.T) {
sc := NewSimpleCondition("", WithPathReg(`\/abc\/.*`))
req := httplib.Post("http://localhost:8080/abc/s")
assert.True(t, sc.Match(context.Background(), req))
req = httplib.Post("http://localhost:8080/abcd/s")
assert.False(t, sc.Match(context.Background(), req))
}

View File

@ -0,0 +1,64 @@
// Copyright 2020 beego
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mock
import (
"context"
"net/http"
"github.com/beego/beego/v2/client/httplib"
)
// MockResponse will return mock response if find any suitable mock data
type MockResponseFilter struct {
ms []*Mock
}
func NewMockResponseFilter() *MockResponseFilter {
return &MockResponseFilter{
ms: make([]*Mock, 0, 1),
}
}
func (m *MockResponseFilter) FilterChain(next httplib.Filter) httplib.Filter {
return func(ctx context.Context, req *httplib.BeegoHTTPRequest) (*http.Response, error) {
for _, mock := range m.ms {
if mock.cond.Match(ctx, req) {
return mock.resp, mock.err
}
}
return next(ctx, req)
}
}
// Mock add mock data
// If the cond.Match(...) = true, the resp and err will be returned
func (m *MockResponseFilter) Mock(cond RequestCondition, resp *http.Response, err error) {
m.ms = append(m.ms, &Mock{
cond: cond,
resp: resp,
err: err,
})
}
type Mock struct {
cond RequestCondition
resp *http.Response
err error
}
type RequestCondition interface {
Match(ctx context.Context, req *httplib.BeegoHTTPRequest) bool
}

View File

@ -0,0 +1,64 @@
// Copyright 2020 beego
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mock
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/beego/beego/v2/client/httplib"
)
func TestMockResponseFilter_FilterChain(t *testing.T) {
req := httplib.Get("http://localhost:8080/abc/s")
ft := NewMockResponseFilter()
expectedResp := NewHttpResponseWithJsonBody(`{}`)
expectedErr := errors.New("expected error")
ft.Mock(NewSimpleCondition("/abc/s"), expectedResp, expectedErr)
req.AddFilters(ft.FilterChain)
resp, err := req.DoRequest()
assert.Equal(t, expectedErr, err)
assert.Equal(t, expectedResp, resp)
req = httplib.Get("http://localhost:8080/abcd/s")
req.AddFilters(ft.FilterChain)
resp, err = req.DoRequest()
assert.NotEqual(t, expectedErr, err)
assert.NotEqual(t, expectedResp, resp)
req = httplib.Get("http://localhost:8080/abc/s")
req.AddFilters(ft.FilterChain)
expectedResp1 := NewHttpResponseWithJsonBody(map[string]string{})
expectedErr1 := errors.New("expected error")
ft.Mock(NewSimpleCondition("/abc/abs/bbc"), expectedResp1, expectedErr1)
resp, err = req.DoRequest()
assert.Equal(t, expectedErr, err)
assert.Equal(t, expectedResp, resp)
req = httplib.Get("http://localhost:8080/abc/abs/bbc")
req.AddFilters(ft.FilterChain)
ft.Mock(NewSimpleCondition("/abc/abs/bbc"), expectedResp1, expectedErr1)
resp, err = req.DoRequest()
assert.Equal(t, expectedErr1, err)
assert.Equal(t, expectedResp1, resp)
}

View File

@ -0,0 +1,38 @@
// Copyright 2020 beego
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mock
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
)
// it will try to convert the data to json format
func NewHttpResponseWithJsonBody(data interface{}) *http.Response {
var body []byte
if str, ok := data.(string); ok {
body = []byte(str)
} else if bts, ok := data.([]byte); ok {
body = bts
} else {
body, _ = json.Marshal(data)
}
return &http.Response{
ContentLength: int64(len(body)),
Body: ioutil.NopCloser(bytes.NewReader(body)),
}
}

View File

@ -21,11 +21,14 @@ import (
logKit "github.com/go-kit/kit/log"
opentracingKit "github.com/go-kit/kit/tracing/opentracing"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
"github.com/beego/beego/v2/client/httplib"
)
type FilterChainBuilder struct {
// TagURL true will tag span with url
TagURL bool
// CustomSpanFunc users are able to custom their span
CustomSpanFunc func(span opentracing.Span, ctx context.Context,
req *httplib.BeegoHTTPRequest, resp *http.Response, err error)
@ -50,13 +53,19 @@ func (builder *FilterChainBuilder) FilterChain(next httplib.Filter) httplib.Filt
}
span.SetTag("http.method", method)
span.SetTag("peer.hostname", req.GetRequest().URL.Host)
span.SetTag("http.url", req.GetRequest().URL.String())
span.SetTag("http.scheme", req.GetRequest().URL.Scheme)
span.SetTag("span.kind", "client")
span.SetTag("component", "beego")
if builder.TagURL {
span.SetTag("http.url", req.GetRequest().URL.String())
}
span.LogFields(log.String("http.url", req.GetRequest().URL.String()))
if err != nil {
span.SetTag("error", true)
span.SetTag("message", err.Error())
span.LogFields(log.String("message", err.Error()))
} else if resp != nil && !(resp.StatusCode < 300 && resp.StatusCode >= 200) {
span.SetTag("error", true)
}

View File

@ -33,7 +33,9 @@ func TestFilterChainBuilder_FilterChain(t *testing.T) {
StatusCode: 404,
}, errors.New("hello")
}
builder := &FilterChainBuilder{}
builder := &FilterChainBuilder{
TagURL: true,
}
filter := builder.FilterChain(next)
req := httplib.Get("https://github.com/notifications?query=repo%3Aastaxie%2Fbeego")
resp, err := filter(context.Background(), req)

View File

@ -338,10 +338,16 @@ func (b *BeegoHTTPRequest) Body(data interface{}) *BeegoHTTPRequest {
case string:
bf := bytes.NewBufferString(t)
b.req.Body = ioutil.NopCloser(bf)
b.req.GetBody = func() (io.ReadCloser, error) {
return ioutil.NopCloser(bf), nil
}
b.req.ContentLength = int64(len(t))
case []byte:
bf := bytes.NewBuffer(t)
b.req.Body = ioutil.NopCloser(bf)
b.req.GetBody = func() (io.ReadCloser, error) {
return ioutil.NopCloser(bf), nil
}
b.req.ContentLength = int64(len(t))
}
return b