-
Notifications
You must be signed in to change notification settings - Fork 10
/
routes_test.go
114 lines (96 loc) · 3.15 KB
/
routes_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
// SPDX-FileCopyrightText: 2017 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package main
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/xmidt-org/themis/key"
)
func TestBuildKeyRoutes(t *testing.T) {
var (
handlerPEM = http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
kid := mux.Vars(request)["kid"]
response.Header().Set("Content-Type", key.ContentTypePEM)
if len(kid) == 0 {
response.WriteHeader(http.StatusInternalServerError)
}
response.Write([]byte("pem"))
})
handlerJWK = http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
kid := mux.Vars(request)["kid"]
response.Header().Set("Content-Type", key.ContentTypeJWK)
if len(kid) == 0 {
response.WriteHeader(http.StatusInternalServerError)
}
response.Write([]byte("jwk"))
})
router = mux.NewRouter()
)
BuildKeyRoutes(KeyRoutesIn{
Router: router,
Handler: handlerPEM,
HandlerJWK: handlerJWK,
})
t.Run("Default", func(t *testing.T) {
var (
assert = assert.New(t)
response = httptest.NewRecorder()
request = httptest.NewRequest("GET", "/keys/test", nil)
)
router.ServeHTTP(response, request)
assert.Equal(http.StatusOK, response.Code)
assert.Equal(key.ContentTypePEM, response.Header().Get("Content-Type"))
assert.Equal("pem", response.Body.String())
})
t.Run("key.pem", func(t *testing.T) {
var (
assert = assert.New(t)
response = httptest.NewRecorder()
request = httptest.NewRequest("GET", "/keys/test/key.pem", nil)
)
router.ServeHTTP(response, request)
assert.Equal(http.StatusOK, response.Code)
assert.Equal(key.ContentTypePEM, response.Header().Get("Content-Type"))
assert.Equal("pem", response.Body.String())
})
t.Run("key.json", func(t *testing.T) {
var (
assert = assert.New(t)
response = httptest.NewRecorder()
request = httptest.NewRequest("GET", "/keys/test/key.json", nil)
)
router.ServeHTTP(response, request)
assert.Equal(http.StatusOK, response.Code)
assert.Equal(key.ContentTypeJWK, response.Header().Get("Content-Type"))
assert.Equal("jwk", response.Body.String())
})
t.Run("Accept", func(t *testing.T) {
t.Run(key.ContentTypePEM, func(t *testing.T) {
var (
assert = assert.New(t)
response = httptest.NewRecorder()
request = httptest.NewRequest("GET", "/keys/test", nil)
)
request.Header.Set("Accept", key.ContentTypePEM)
router.ServeHTTP(response, request)
assert.Equal(http.StatusOK, response.Code)
assert.Equal(key.ContentTypePEM, response.Header().Get("Content-Type"))
assert.Equal("pem", response.Body.String())
})
t.Run(key.ContentTypeJWK, func(t *testing.T) {
var (
assert = assert.New(t)
response = httptest.NewRecorder()
request = httptest.NewRequest("GET", "/keys/test", nil)
)
request.Header.Set("Accept", key.ContentTypeJWK)
router.ServeHTTP(response, request)
assert.Equal(http.StatusOK, response.Code)
assert.Equal(key.ContentTypeJWK, response.Header().Get("Content-Type"))
assert.Equal("jwk", response.Body.String())
})
})
}