forked from foomo/soap
-
Notifications
You must be signed in to change notification settings - Fork 17
/
server_test.go
88 lines (76 loc) · 2.53 KB
/
server_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
package soap
import (
"bytes"
"encoding/xml"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestServer_ServeHTTP(t *testing.T) {
soapSrv := NewServer()
soapSrv.UseSoap11() // just for testing
soapSrv.RegisterHandler(
"/pathTo",
"testPostAction",
"fooRequest",
func() interface{} {
return &FooRequest{}
},
func(request interface{}, w http.ResponseWriter, httpRequest *http.Request) (interface{}, error) {
fooRequest := request.(*FooRequest)
return &FooResponse{
Bar: "Hello \"" + fooRequest.Foo + "\"",
}, nil
},
)
srv := httptest.NewServer(soapSrv)
defer srv.Close()
// the NewClient is incompatible to the server. why? because of the SOAP
// namespace and its check. due to backwards compatibility reasons the
// structs in this package can't be changed.
postFn := func(t *testing.T, postBody []byte) *http.Response {
body := ioutil.NopCloser(bytes.NewReader(postBody))
req, err := http.NewRequest("POST", srv.URL+"/pathTo", body)
require.NoError(t, err)
req.Header.Add("Content-Type", SoapContentType11)
req.Header.Add("SOAPAction", "testPostAction")
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
return resp
}
t.Run("request succeeds", func(t *testing.T) {
resp := postFn(t, []byte(`<SOAP:Envelope xmlns:SOAP="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>i am foo</Foo>
</fooRequest>
</Body>
</SOAP:Envelope>`))
responseEnvelope := &Envelope{
Body: Body{
Content: &FooResponse{},
},
}
require.NoError(t, xml.NewDecoder(resp.Body).Decode(responseEnvelope))
assert.Exactly(t, "Hello \"i am foo\"", responseEnvelope.Body.Content.(*FooResponse).Bar)
})
t.Run("request failed", func(t *testing.T) {
resp := postFn(t, []byte(`<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
<Header xmlns="http://schemas.xmlsoap.org/soap/envelope/"></Header>
<Body xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<barRequest>
<Foo>i am foo</Foo>
</barRequest>
</Body>
</SOAP:Envelope>`))
responseEnvelope := &Envelope{
Body: Body{Content: &dummyContent{}},
}
require.NoError(t, xml.NewDecoder(resp.Body).Decode(responseEnvelope))
assert.Exactly(t, "no action handler for content type: \"barRequest\"", responseEnvelope.Body.Fault.String)
})
}