-
Notifications
You must be signed in to change notification settings - Fork 0
/
gors_test.go
67 lines (53 loc) · 1.6 KB
/
gors_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
package gors
import (
"context"
"net/http"
"net/http/httptest"
"testing"
)
func TestServeHTTP204StatusCodeWhenPluginIsEnabled(t *testing.T) {
// Gors is enabled and should return 204 for OPTIONS request
cfg := Config{
Disabled: false,
}
ctx := context.Background()
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})
gors, err := New(ctx, next, &cfg, "test-traefik-gors-plugin")
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
req, err := http.NewRequestWithContext(ctx, http.MethodOptions, "http://localhost", nil)
if err != nil {
t.Fatal(err)
}
gors.ServeHTTP(recorder, req)
if recorder.Code != http.StatusNoContent {
t.Fatal("Expected status code: ", http.StatusNoContent, " - Got: ", recorder.Code)
}
}
func TestServeHTTP200StatusCodeWhenPluginIsDisabled(t *testing.T) {
// Gors is disabled and should pass through the request to `next` no matter what the request is
cfg := Config{
Disabled: true,
}
ctx := context.Background()
nextCalled := false
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { nextCalled = true; rw.WriteHeader(http.StatusOK) })
gors, err := New(ctx, next, &cfg, "test-traefik-gors-plugin")
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
req, err := http.NewRequestWithContext(ctx, http.MethodOptions, "http://localhost", nil)
if err != nil {
t.Fatal(err)
}
gors.ServeHTTP(recorder, req)
if !nextCalled {
t.Fatal("Next did not called")
}
if recorder.Code != http.StatusOK {
t.Fatal("Expected status code: ", http.StatusOK, " - Got: ", recorder.Code)
}
}