1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
// Copyright 2017 PingCAP, Inc.
//
// 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 fn
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/stretchr/testify/require"
)
type testRequest struct {
Foo string `json:"foo"`
Bar int `json:"bar"`
}
type testResponse struct {
Code int `json:"code"`
Message string `json:"message"`
}
type testErrorResponse struct {
Code int `json:"code"`
Error string `json:"message"`
}
var successResponse = &testResponse{Message: "success"}
func init() {
Plugin(
func(ctx context.Context, request *http.Request) (context.Context, error) {
return context.WithValue(ctx, "global1", "globalvalue1"), nil
},
nil,
func(ctx context.Context, request *http.Request) (context.Context, error) {
return context.WithValue(ctx, "global2", "globalvalue2"), nil
})
}
// acceptable function signature
func withNone() (*testResponse, error) { return successResponse, nil }
func withBody(io.ReadCloser) (*testResponse, error) { return successResponse, nil }
func withReq(*testRequest) (*testResponse, error) { return successResponse, nil }
func withHeader(http.Header) (*testResponse, error) { return successResponse, nil }
func withForm(Form) (*testResponse, error) { return successResponse, nil }
func withPostForm(PostForm) (*testResponse, error) { return successResponse, nil }
func withFormPtr(*Form) (*testResponse, error) { return successResponse, nil }
func withPostFormPtr(*PostForm) (*testResponse, error) { return successResponse, nil }
func withMultipartForm(*multipart.Form) (*testResponse, error) { return successResponse, nil }
func withUrl(*url.URL) (*testResponse, error) { return successResponse, nil }
func withRawRequest(*http.Request) (*testResponse, error) { return successResponse, nil }
func withInContext(context.Context) (*testResponse, error) { return successResponse, nil }
func withInContextAndPayload(context.Context, *testRequest) (*testResponse, error) {
return successResponse, nil
}
func withMulti(*testRequest, Form, PostForm, http.Header, *url.URL) (*testResponse, error) {
return nil, nil
}
func withAll(io.ReadCloser, *testRequest, Form, PostForm, http.Header, *multipart.Form, *url.URL) (*testResponse, error) {
return nil, nil
}
func TestHandler(t *testing.T) {
Wrap(withNone)
Wrap(withBody)
Wrap(withReq)
Wrap(withHeader)
Wrap(withForm)
Wrap(withPostForm)
Wrap(withFormPtr)
Wrap(withPostFormPtr)
Wrap(withMultipartForm)
Wrap(withUrl)
Wrap(withRawRequest)
Wrap(withMulti)
Wrap(withAll)
Wrap(withInContext)
Wrap(withInContextAndPayload)
}
func TestPlugin(t *testing.T) {
logic := func(ctx context.Context) (*testResponse, error) {
require.Equal(t, "value", ctx.Value("key").(string))
require.Equal(t, "value2", ctx.Value("key2").(string))
return &testResponse{}, nil
}
plugin1 := func(ctx context.Context, request *http.Request) (context.Context, error) {
require.Equal(t, "globalvalue1", ctx.Value("global1").(string))
require.Equal(t, "globalvalue2", ctx.Value("global2").(string))
return context.WithValue(ctx, "key", "value"), nil
}
plugin2 := func(ctx context.Context, request *http.Request) (context.Context, error) {
require.Equal(t, "globalvalue1", ctx.Value("global1").(string))
require.Equal(t, "globalvalue2", ctx.Value("global2").(string))
require.Equal(t, "value", ctx.Value("key").(string))
return context.WithValue(ctx, "key2", "value2"), nil
}
handler := Wrap(logic).Plugin(plugin1, plugin2)
recorder := httptest.NewRecorder()
request, err := http.NewRequest(http.MethodGet, "", nil)
require.NoError(t, err)
handler.ServeHTTP(recorder, request)
}
func TestGroupPlugin(t *testing.T) {
group := NewGroup()
group.Plugin(func(ctx context.Context, request *http.Request) (context.Context, error) {
return context.WithValue(ctx, "key", "value"), nil
})
logic := func(ctx context.Context) (*testResponse, error) {
require.Equal(t, "value", ctx.Value("key").(string))
return &testResponse{}, nil
}
handler := group.Wrap(logic)
recorder := httptest.NewRecorder()
request, err := http.NewRequest(http.MethodGet, "", nil)
require.NoError(t, err)
handler.ServeHTTP(recorder, request)
}
func TestSetResponseEncoder(t *testing.T) {
handler := Wrap(func(ctx context.Context, request *http.Request) (context.Context, error) {
return context.TODO(), nil
})
testResp := &testResponse{
Code: 1,
Message: "msg",
}
SetResponseEncoder(func(ctx context.Context, payload interface{}) interface{} {
return testResp
})
recorder := httptest.NewRecorder()
request, err := http.NewRequest(http.MethodGet, "", nil)
require.NoError(t, err)
handler.ServeHTTP(recorder, request)
respMsg := &testResponse{}
_ = json.Unmarshal(recorder.Body.Bytes(), &respMsg)
require.Equal(t, testResp, respMsg)
}
func TestSetErrorEncoder(t *testing.T) {
handler := Wrap(func(ctx context.Context, request *http.Request) (context.Context, error) {
return nil, errors.New("")
})
testErrorResp := &testErrorResponse{
Code: -1,
Error: "something went wrong",
}
SetErrorEncoder(func(ctx context.Context, err error) interface{} {
return testErrorResp
})
recorder := httptest.NewRecorder()
request, err := http.NewRequest(http.MethodGet, "", nil)
require.NoError(t, err)
handler.ServeHTTP(recorder, request)
respMsg := &testErrorResponse{}
_ = json.Unmarshal(recorder.Body.Bytes(), &respMsg)
require.Equal(t, testErrorResp, respMsg)
}
func TestGenericAdapter_Invoke(t *testing.T) {
type CustomForm testRequest
handler := Wrap(func(ctx context.Context, form *CustomForm) (context.Context, error) {
return nil, nil
})
recorder := httptest.NewRecorder()
request, err := http.NewRequest(http.MethodGet, "", nil)
require.NoError(t, err)
payload := []byte(`{"for":"hello", "bar":10000}`)
request.Body = ioutil.NopCloser(bytes.NewBuffer(payload))
require.NoError(t, err)
handler.ServeHTTP(recorder, request)
}
func TestSimpleUnaryAdapter_Invoke(t *testing.T) {
handler := Wrap(withReq)
recorder := httptest.NewRecorder()
request, err := http.NewRequest(http.MethodGet, "", nil)
require.NoError(t, err)
payload := []byte(`{"for":"hello", "bar":10000}`)
request.Body = ioutil.NopCloser(bytes.NewBuffer(payload))
require.NoError(t, err)
handler.ServeHTTP(recorder, request)
}
func TestErrorWithStatusCode(t *testing.T) {
handler := Wrap(func(ctx context.Context, request *http.Request) (context.Context, error) {
return nil, ErrorWithStatusCode(errors.New("not found"), http.StatusNotFound)
})
recorder := httptest.NewRecorder()
request, err := http.NewRequest(http.MethodGet, "", nil)
require.NoError(t, err)
handler.ServeHTTP(recorder, request)
require.Equal(t, http.StatusNotFound, recorder.Code)
}
func BenchmarkSimplePlainAdapter_Invoke(b *testing.B) {
handler := Wrap(withNone)
request, err := http.NewRequest(http.MethodGet, "", nil)
if err != nil {
b.Fatal(err)
}
recorder := httptest.NewRecorder()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
handler.ServeHTTP(recorder, request)
}
}
func BenchmarkSimpleUnaryAdapter_Invoke(b *testing.B) {
handler := Wrap(withReq)
request, err := http.NewRequest(http.MethodGet, "", nil)
if err != nil {
b.Fatal(err)
}
payload := []byte(`{"for":"hello", "bar":10000}`)
request.Body = ioutil.NopCloser(bytes.NewBuffer(payload))
recorder := httptest.NewRecorder()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
handler.ServeHTTP(recorder, request)
}
}
func BenchmarkGenericAdapter_Invoke(b *testing.B) {
handler := Wrap(withMulti)
request, err := http.NewRequest(http.MethodGet, "", nil)
if err != nil {
b.Fatal(err)
}
payload := []byte(`{"for":"hello", "bar":10000}`)
request.Body = ioutil.NopCloser(bytes.NewBuffer(payload))
recorder := httptest.NewRecorder()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
handler.ServeHTTP(recorder, request)
}
}
|