forked from globusdigital/soap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
166 lines (152 loc) · 5.31 KB
/
client_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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package soap
import (
"bytes"
"context"
"encoding/xml"
"io/ioutil"
"mime/multipart"
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type FooRequest struct {
XMLName xml.Name `xml:"fooRequest"`
Foo string
}
// FooResponse a simple response
type FooResponse struct {
Bar string
}
func TestClient_Call(t *testing.T) {
wantSOAPBody := []byte(`<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<Header xmlns="http://schemas.xmlsoap.org/soap/envelope/"></Header>
<Body xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<fooRequest>
<Foo>hello world</Foo>
</fooRequest>
</Body>
</Envelope>`)
httpSOAPResponse := []byte(`<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<FooResponse xmlns="http://xmlme.com/WebServices">
<Bar>I love deadlines. I like the whooshing sound they make as they fly by.</Bar>
</FooResponse>
</soap12:Body>
</soap12:Envelope>`)
clientDoFn := func(rt func(r *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) {
return (&http.Client{
Transport: RoundTrip(rt),
}).Do
}
t.Run("without multipart", func(t *testing.T) {
t.Run("success", func(t *testing.T) {
c := NewClient("http://localhorst.ch", &BasicAuth{
Login: "test",
Password: "test",
})
c.UserAgent = "ncc-1701-d"
c.RequestHeaderFn = func(header http.Header) {
header.Set("X-Answer", "42")
}
c.HTTPClientDoFn = clientDoFn(func(r *http.Request) (*http.Response, error) {
haveBody, _ := ioutil.ReadAll(r.Body)
assert.Exactly(t, wantSOAPBody, haveBody)
assert.Exactly(t, "42", r.Header.Get("X-Answer"))
assert.Exactly(t, "ncc-1701-d", r.Header.Get("User-Agent"))
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewReader(httpSOAPResponse)),
}, nil
})
req := FooRequest{
Foo: "hello world",
}
var resp FooResponse
httpResp, err := c.Call(context.Background(), "MySOAPAction", &req, &resp)
require.NoError(t, err)
assert.NotNil(t, httpResp)
assert.Exactly(t, 200, httpResp.StatusCode)
assert.Exactly(t, FooResponse{Bar: `I love deadlines. I like the whooshing sound they make as they fly by.`}, resp)
})
t.Run("no soap body", func(t *testing.T) {
c := NewClient("http://localhorst.ch", nil)
c.HTTPClientDoFn = clientDoFn(func(r *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(strings.NewReader(`<?xml version="1.0" encoding="utf-8"?>
<seife12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<seife:Body></seife:Body>
</seife:Envelope>`)),
}, nil
})
req := FooRequest{}
var resp FooResponse
httpResp, err := c.Call(context.Background(), "MySOAPAction", &req, &resp)
assert.Nil(t, httpResp)
assert.EqualError(t, err, "this is not a 1.1 SOAP-Message: \"<?xml version=\\\"1.0\\\" encoding=\\\"utf-8\\\"?>\\n<seife12:Envelope xmlns:xsi=\\\"http://www.w3.org/2001/XMLSchema-instance\\\"\\n xmlns:xsd=\\\"http://www.w3.org/2001/XMLSchema\\\" \\n xmlns:soap12=\\\"http://www.w3.org/2003/05/soap-envelope\\\">\\n <seife:Body></seife:Body>\\n</seife:Envelope>\"")
})
})
t.Run("with multipart", func(t *testing.T) {
t.Run("success", func(t *testing.T) {
c := NewClient("http://localhorst.ch", nil)
c.HTTPClientDoFn = clientDoFn(func(r *http.Request) (*http.Response, error) {
buf, mw := createMultiPart(t, httpSOAPResponse)
hdr := http.Header{}
hdr.Add("Content-Type", mw.FormDataContentType())
return &http.Response{
Header: hdr,
StatusCode: 200,
Body: ioutil.NopCloser(buf),
}, nil
})
req := FooRequest{
Foo: "hello world",
}
var resp FooResponse
httpResp, err := c.Call(context.Background(), "MySOAPAction", &req, &resp)
require.NoError(t, err)
assert.NotNil(t, httpResp)
assert.Exactly(t, 200, httpResp.StatusCode)
assert.Exactly(t, FooResponse{Bar: `I love deadlines. I like the whooshing sound they make as they fly by.`}, resp)
})
t.Run("no soap found", func(t *testing.T) {
c := NewClient("http://localhorst.ch", nil)
c.HTTPClientDoFn = clientDoFn(func(r *http.Request) (*http.Response, error) {
buf, mw := createMultiPart(t, []byte(`<wrong></wrong>`))
hdr := http.Header{}
hdr.Add("Content-Type", mw.FormDataContentType())
return &http.Response{
Header: hdr,
StatusCode: 200,
Body: ioutil.NopCloser(buf),
}, nil
})
req := FooRequest{
Foo: "hello world",
}
var resp FooResponse
httpResp, err := c.Call(context.Background(), "MySOAPAction", &req, &resp)
assert.Nil(t, httpResp)
assert.EqualError(t, err, "multipart message does contain a soapy part")
})
})
}
func createMultiPart(t *testing.T, data []byte) (*bytes.Buffer, *multipart.Writer) {
buf := new(bytes.Buffer)
w := multipart.NewWriter(buf)
fw, err := w.CreateFormFile("soap", "test_soap.xml")
if err != nil {
t.Fatal(err)
}
fw.Write(data)
// Important if you do not close the multipart writer you will not have a
// terminating boundry
w.Close()
return buf, w
}