forked from cloudfoundry-incubator/bits-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign_handler_test.go
63 lines (51 loc) · 1.98 KB
/
sign_handler_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
package bitsgo_test
import (
"net/http"
"net/http/httptest"
"reflect"
"time"
"github.com/cloudfoundry-incubator/bits-service"
"github.com/cloudfoundry-incubator/bits-service/httputil"
. "github.com/petergtz/pegomock"
)
func AnyTime() (result time.Time) {
RegisterMatcher(NewAnyMatcher(reflect.TypeOf((*time.Time)(nil)).Elem()))
return
}
var _ = Describe("SignResourceHandler", func() {
var (
getSigner *MockResourceSigner
putSigner *MockResourceSigner
recorder *httptest.ResponseRecorder
)
BeforeEach(func() {
getSigner = NewMockResourceSigner()
putSigner = NewMockResourceSigner()
recorder = httptest.NewRecorder()
})
It("Signs a GET URL", func() {
When(getSigner.Sign(AnyString(), AnyString(), AnyTime())).ThenReturn("Some get signature")
handler := bitsgo.NewSignResourceHandler(getSigner, putSigner)
request := httputil.NewRequest("GET", "/foo", nil).Build()
handler.Sign(recorder, request, map[string]string{"verb": "get", "resource": "bar"})
Expect(recorder.Code).To(Equal(http.StatusOK))
Expect(recorder.Body.String()).To(Equal("Some get signature"))
})
Context("Invalid method", func() {
It("Responds with error code", func() {
handler := bitsgo.NewSignResourceHandler(getSigner, putSigner)
request := httputil.NewRequest("", "/does", nil).Build()
handler.Sign(recorder, request, map[string]string{"verb": "something invalid", "resource": "foobar"})
Expect(recorder.Code).To(Equal(http.StatusBadRequest))
Expect(recorder.Body.String()).To(Equal("Invalid verb: something invalid"))
})
})
It("Signs a PUT URL", func() {
When(putSigner.Sign(AnyString(), AnyString(), AnyTime())).ThenReturn("Some put signature")
handler := bitsgo.NewSignResourceHandler(getSigner, putSigner)
request := httputil.NewRequest("PUT", "/bar", nil).Build()
handler.Sign(recorder, request, map[string]string{"verb": "put", "resource": "foobar"})
Expect(recorder.Code).To(Equal(http.StatusOK))
Expect(recorder.Body.String()).To(Equal("Some put signature"))
})
})