/
endpoint_test.go
116 lines (98 loc) · 3.3 KB
/
endpoint_test.go
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
package gin
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/devopsfaith/krakend/config"
"github.com/devopsfaith/krakend/proxy"
)
func TestEndpointHandler_ok(t *testing.T) {
p := func(_ context.Context, _ *proxy.Request) (*proxy.Response, error) {
return &proxy.Response{
IsComplete: true,
Data: map[string]interface{}{"supu": "tupu"},
}, nil
}
expectedBody := "{\"supu\":\"tupu\"}"
testEndpointHandler(t, 10, p, expectedBody, "public, max-age=21600", "application/json; charset=utf-8", http.StatusOK)
}
func TestEndpointHandler_incomplete(t *testing.T) {
p := func(_ context.Context, _ *proxy.Request) (*proxy.Response, error) {
return &proxy.Response{
IsComplete: false,
Data: map[string]interface{}{"foo": "bar"},
}, nil
}
expectedBody := "{\"foo\":\"bar\"}"
testEndpointHandler(t, 10, p, expectedBody, "", "application/json; charset=utf-8", http.StatusOK)
}
func TestEndpointHandler_ko(t *testing.T) {
p := func(_ context.Context, _ *proxy.Request) (*proxy.Response, error) {
return nil, fmt.Errorf("This is %s", "a dummy error")
}
testEndpointHandler(t, 10, p, "", "", "", http.StatusInternalServerError)
}
func TestEndpointHandler_cancel(t *testing.T) {
p := func(_ context.Context, _ *proxy.Request) (*proxy.Response, error) {
time.Sleep(100 * time.Millisecond)
return nil, nil
}
testEndpointHandler(t, 0, p, "", "", "", http.StatusInternalServerError)
}
func TestEndpointHandler_noop(t *testing.T) {
testEndpointHandler(t, 10, proxy.NoopProxy, "{}", "", "application/json; charset=utf-8", http.StatusOK)
}
func testEndpointHandler(t *testing.T, timeout time.Duration, p proxy.Proxy, expectedBody, expectedCache,
expectedContent string, expectedStatusCode int) {
body, resp, err := setup(timeout, p)
if err != nil {
t.Error("Reading the response:", err.Error())
return
}
content := string(body)
if resp.Header.Get("Cache-Control") != expectedCache {
t.Error("Cache-Control error:", resp.Header.Get("Cache-Control"))
}
if resp.Header.Get("Content-Type") != expectedContent {
t.Error("Content-Type error:", resp.Header.Get("Content-Type"))
}
if resp.Header.Get("X-Krakend") != "Version undefined" {
t.Error("X-Krakend error:", resp.Header.Get("X-Krakend"))
}
if resp.StatusCode != expectedStatusCode {
t.Error("Unexpected status code:", resp.StatusCode)
}
if content != expectedBody {
t.Error("Unexpected body:", content, "expected:", expectedBody)
}
}
func setup(timeout time.Duration, p proxy.Proxy) (string, *http.Response, error) {
endpoint := &config.EndpointConfig{
Timeout: timeout,
CacheTTL: 6 * time.Hour,
QueryString: []string{"b"},
}
server := startGinServer(EndpointHandler(endpoint, p))
req, _ := http.NewRequest("GET", "http://127.0.0.1:8080/_gin_endpoint/a?b=1", ioutil.NopCloser(&bytes.Buffer{}))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
server.ServeHTTP(w, req)
defer w.Result().Body.Close()
body, ioerr := ioutil.ReadAll(w.Result().Body)
if ioerr != nil {
return "", nil, ioerr
}
return string(body), w.Result(), nil
}
func startGinServer(handlerFunc gin.HandlerFunc) *gin.Engine {
gin.SetMode(gin.TestMode)
router := gin.New()
router.GET("/_gin_endpoint/:param", handlerFunc)
return router
}