/
endpoint_test.go
146 lines (130 loc) · 4.94 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
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
package mux
import (
"bytes"
"context"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/devopsfaith/krakend/config"
"github.com/devopsfaith/krakend/proxy"
"github.com/devopsfaith/krakend/router"
)
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, "GET", expectedBody, "public, max-age=21600", "application/json", http.StatusOK, true)
time.Sleep(5 * time.Millisecond)
}
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, "GET", expectedBody, "", "application/json", http.StatusOK, false)
time.Sleep(5 * time.Millisecond)
}
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, "GET", "This is a dummy error\n", "", "text/plain; charset=utf-8", http.StatusInternalServerError, false)
time.Sleep(5 * time.Millisecond)
}
func TestEndpointHandler_incompleteAndErrored(t *testing.T) {
p := func(_ context.Context, _ *proxy.Request) (*proxy.Response, error) {
return &proxy.Response{
IsComplete: false,
Data: map[string]interface{}{"foo": "bar"},
}, errors.New("This is a dummy error")
}
expectedBody := "{\"foo\":\"bar\"}"
testEndpointHandler(t, 10, p, "GET", expectedBody, "", "application/json", http.StatusOK, false)
time.Sleep(5 * time.Millisecond)
}
func TestEndpointHandler_cancel(t *testing.T) {
p := func(_ context.Context, _ *proxy.Request) (*proxy.Response, error) {
time.Sleep(100 * time.Millisecond)
return &proxy.Response{
IsComplete: false,
Data: map[string]interface{}{"foo": "bar"},
}, nil
}
testEndpointHandler(t, 0, p, "GET", "{\"foo\":\"bar\"}", "", "application/json", http.StatusOK, false)
time.Sleep(5 * time.Millisecond)
}
func TestEndpointHandler_cancelEmpty(t *testing.T) {
p := func(_ context.Context, _ *proxy.Request) (*proxy.Response, error) {
time.Sleep(100 * time.Millisecond)
return nil, nil
}
testEndpointHandler(t, 0, p, "GET", router.ErrInternalError.Error()+"\n", "", "text/plain; charset=utf-8", http.StatusInternalServerError, false)
time.Sleep(5 * time.Millisecond)
}
func TestEndpointHandler_noop(t *testing.T) {
testEndpointHandler(t, 10, proxy.NoopProxy, "GET", "{}", "", "application/json", http.StatusOK, false)
time.Sleep(5 * time.Millisecond)
}
func TestEndpointHandler_badMethod(t *testing.T) {
testEndpointHandler(t, 10, proxy.NoopProxy, "PUT", "\n", "", "text/plain; charset=utf-8", http.StatusMethodNotAllowed, false)
time.Sleep(5 * time.Millisecond)
}
func testEndpointHandler(t *testing.T, timeout time.Duration, p proxy.Proxy, method, expectedBody, expectedCache, expectedContent string,
expectedStatusCode int, completed bool) {
endpoint := &config.EndpointConfig{
Method: "GET",
Timeout: timeout,
CacheTTL: 6 * time.Hour,
QueryString: []string{"b"},
}
server := startMuxServer(EndpointHandler(endpoint, p))
req, _ := http.NewRequest(method, "http://127.0.0.1:8081/_mux_endpoint?b=1", ioutil.NopCloser(&bytes.Buffer{}))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
server.ServeHTTP(w, req)
body, ioerr := ioutil.ReadAll(w.Result().Body)
if ioerr != nil {
t.Error("Reading the response:", ioerr.Error())
return
}
w.Result().Body.Close()
content := string(body)
resp := w.Result()
if resp.Header.Get("Cache-Control") != expectedCache {
t.Error("Cache-Control error:", resp.Header.Get("Cache-Control"))
}
if completed && resp.Header.Get(router.CompleteResponseHeaderName) != router.HeaderCompleteResponseValue {
t.Error(router.CompleteResponseHeaderName, "error:", resp.Header.Get(router.CompleteResponseHeaderName))
}
if !completed && resp.Header.Get(router.CompleteResponseHeaderName) != router.HeaderIncompleteResponseValue {
t.Error(router.CompleteResponseHeaderName, "error:", resp.Header.Get(router.CompleteResponseHeaderName))
}
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 startMuxServer(handlerFunc http.HandlerFunc) *http.ServeMux {
router := http.NewServeMux()
router.Handle("/_mux_endpoint", handlerFunc)
return router
}