forked from arachnys/athenapdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware_test.go
143 lines (135 loc) · 4.17 KB
/
middleware_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
package main
import (
"errors"
"github.com/arachnys/athenapdf/weaver/converter"
"github.com/getsentry/raven-go"
"github.com/gin-gonic/gin"
"gopkg.in/alexcesaro/statsd.v2"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"time"
)
func TestConfigMiddleware(t *testing.T) {
r := gin.Default()
mockConf := Config{MaxWorkers: 17}
var ctxConf Config
r.Use(ConfigMiddleware(mockConf))
r.GET("/", func(c *gin.Context) {
ctxConf = c.MustGet("config").(Config)
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)
r.ServeHTTP(res, req)
if got, want := res.Code, http.StatusOK; got != want {
t.Fatalf("expected response code to be %d, got %d", want, got)
}
if !reflect.DeepEqual(mockConf, ctxConf) {
t.Errorf("expected config in context to be %+v, got %+v", mockConf, ctxConf)
}
}
func TestWorkQueueMiddleware(t *testing.T) {
r := gin.Default()
mockWq := make(chan converter.Work, 17)
var ctxWq chan<- converter.Work
r.Use(WorkQueueMiddleware(mockWq))
r.GET("/", func(c *gin.Context) {
ctxWq = c.MustGet("queue").(chan<- converter.Work)
go func(wq chan<- converter.Work) {
ctxWq <- converter.Work{}
}(ctxWq)
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)
r.ServeHTTP(res, req)
if got, want := res.Code, http.StatusOK; got != want {
t.Fatalf("expected response code to be %d, got %d", want, got)
}
select {
case <-mockWq:
case <-time.After(time.Second):
t.Errorf("expected to receive work from queue before timeout")
}
}
func TestSentryMiddleware(t *testing.T) {
r := gin.Default()
mockRaven := new(raven.Client)
var ctxRaven *raven.Client
r.Use(SentryMiddleware(mockRaven))
r.GET("/", func(c *gin.Context) {
ctxRaven = c.MustGet("sentry").(*raven.Client)
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)
r.ServeHTTP(res, req)
if got, want := res.Code, http.StatusOK; got != want {
t.Fatalf("expected response code to be %d, got %d", want, got)
}
if !reflect.DeepEqual(mockRaven, ctxRaven) {
t.Errorf("expected sentry in context to be %+v, got %+v", mockRaven, ctxRaven)
}
}
func TestStatsdMiddleware(t *testing.T) {
r := gin.Default()
mockStatsd := new(statsd.Client)
var ctxStatsd *statsd.Client
r.Use(StatsdMiddleware(mockStatsd))
r.GET("/", func(c *gin.Context) {
ctxStatsd = c.MustGet("statsd").(*statsd.Client)
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)
r.ServeHTTP(res, req)
if got, want := res.Code, http.StatusOK; got != want {
t.Fatalf("expected response code to be %d, got %d", want, got)
}
if !reflect.DeepEqual(mockStatsd, ctxStatsd) {
t.Errorf("expected statsd in context to be %+v, got %+v", mockStatsd, ctxStatsd)
}
}
func TestErrorMiddleware(t *testing.T) {
r := gin.Default()
r.Use(ErrorMiddleware())
r.GET("/", func(c *gin.Context) {
c.Error(errors.New("test error"))
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)
r.ServeHTTP(res, req)
if got, want := res.Code, http.StatusInternalServerError; got != want {
t.Fatalf("expected response code to be %d, got %d", want, got)
}
got, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatalf("unable to read response body: %+v", err)
}
want := "{\"error\":\"PDF conversion failed due to an internal server error\"}"
if !reflect.DeepEqual(strings.TrimSpace(string(got)), want) {
t.Errorf("expected response body to be %s, got %s", want, got)
}
}
func TestAuthorizationMiddleware(t *testing.T) {
r := gin.Default()
r.Use(AuthorizationMiddleware("123456"))
r.GET("/", func(c *gin.Context) {})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/?auth=123456", nil)
r.ServeHTTP(res, req)
if got, want := res.Code, http.StatusOK; got != want {
t.Fatalf("expected response code to be %d, got %d", want, got)
}
}
func TestAuthorizationMiddleware_authFailure(t *testing.T) {
r := gin.Default()
r.Use(AuthorizationMiddleware("123456"))
r.GET("/", func(c *gin.Context) {})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)
r.ServeHTTP(res, req)
if got, want := res.Code, http.StatusUnauthorized; got != want {
t.Fatalf("expected response code to be %d, got %d", want, got)
}
}