-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathcurl_test.go
More file actions
91 lines (77 loc) · 2.62 KB
/
Copy pathcurl_test.go
File metadata and controls
91 lines (77 loc) · 2.62 KB
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
package uaa_test
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
uaa "github.com/cloudfoundry-community/go-uaa"
. "github.com/onsi/gomega"
"github.com/sclevine/spec"
)
func testCurl(t *testing.T, when spec.G, it spec.S) {
var (
s *httptest.Server
handler http.Handler
called int
a *uaa.API
)
it.Before(func() {
RegisterTestingT(t)
called = 0
s = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
called = called + 1
Expect(handler).NotTo(BeNil())
handler.ServeHTTP(w, req)
}))
a, _ = uaa.New(s.URL, uaa.WithNoAuthentication())
})
it.After(func() {
if s != nil {
s.Close()
}
})
it("gets a user from the UAA by id", func() {
handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
Expect(req.Header.Get("Accept")).To(Equal("application/json"))
Expect(req.URL.Path).To(Equal("/Users/00000000-0000-0000-0000-000000000001"))
Expect(req.Method).To(Equal(http.MethodGet))
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(userResponse))
Expect(err).NotTo(HaveOccurred())
})
_, resBody, status, err := a.Curl("/Users/00000000-0000-0000-0000-000000000001", "GET", "", []string{"Accept: application/json"})
Expect(err).NotTo(HaveOccurred())
var user uaa.User
err = json.Unmarshal([]byte(resBody), &user)
Expect(err).NotTo(HaveOccurred())
Expect(user.ID).To(Equal("00000000-0000-0000-0000-000000000001"))
Expect(status).To(Equal(http.StatusOK))
})
it("can POST body and multiple headers", func() {
reqBody := map[string]interface{}{
"externalID": "marcus-user",
"userName": "marcus@stoicism.com",
}
reqBodyBytes, err := json.Marshal(reqBody)
handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
Expect(req.Header.Get("Accept")).To(Equal("application/json"))
Expect(req.Header.Get("Content-Type")).To(Equal("application/json"))
Expect(req.Method).To(Equal(http.MethodPost))
Expect(req.URL.Path).To(Equal("/Users"))
defer req.Body.Close()
body, _ := ioutil.ReadAll(req.Body)
Expect(body).To(MatchJSON(reqBodyBytes))
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(userResponse))
Expect(err).NotTo(HaveOccurred())
})
Expect(err).NotTo(HaveOccurred())
_, resBody, status, _ := a.Curl("/Users", "POST", string(reqBodyBytes), []string{"Content-Type: application/json", "Accept: application/json"})
var user uaa.User
err = json.Unmarshal([]byte(resBody), &user)
Expect(err).NotTo(HaveOccurred())
Expect(user.ID).To(Equal("00000000-0000-0000-0000-000000000001"))
Expect(status).To(Equal(http.StatusOK))
})
}