forked from hooklift/gowsdl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoap_test.go
147 lines (127 loc) · 3.87 KB
/
soap_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
package soap
import (
"bytes"
"encoding/xml"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
type Ping struct {
XMLName xml.Name `xml:"http://example.com/service.xsd Ping"`
Request *PingRequest `xml:"request,omitempty"`
}
type PingRequest struct {
// XMLName xml.Name `xml:"http://example.com/service.xsd PingRequest"`
Message string `xml:"Message,omitempty"`
Attachment *Binary `xml:"Attachment,omitempty"`
}
type PingResponse struct {
XMLName xml.Name `xml:"http://example.com/service.xsd PingResponse"`
PingResult *PingReply `xml:"PingResult,omitempty"`
}
type PingReply struct {
// XMLName xml.Name `xml:"http://example.com/service.xsd PingReply"`
Message string `xml:"Message,omitempty"`
Attachment []byte `xml:"Attachment,omitempty"`
}
func TestClient_Call(t *testing.T) {
var pingRequest = new(Ping)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
xml.NewDecoder(r.Body).Decode(pingRequest)
rsp := `<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<PingResponse xmlns="http://example.com/service.xsd">
<PingResult>
<Message>Pong hi</Message>
</PingResult>
</PingResponse>
</soap:Body>
</soap:Envelope>`
w.Write([]byte(rsp))
}))
defer ts.Close()
client := NewClient(ts.URL)
req := &Ping{Request: &PingRequest{Message: "Hi"}}
reply := &PingResponse{}
if err := client.Call("GetData", req, reply); err != nil {
t.Fatalf("couln't call service: %v", err)
}
wantedMsg := "Pong hi"
if reply.PingResult.Message != wantedMsg {
t.Errorf("got msg %s wanted %s", reply.PingResult.Message, wantedMsg)
}
}
func TestClient_Send_Correct_Headers(t *testing.T) {
tests := []struct {
action string
reqHeaders map[string]string
expectedHeaders map[string]string
}{
// default case when no custom headers are set
{
"GetTrade",
map[string]string{},
map[string]string{
"User-Agent": "gowsdl/0.1",
"SOAPAction": "GetTrade",
"Content-Type": "text/xml; charset=\"utf-8\"",
},
},
// override default User-Agent
{
"SaveTrade",
map[string]string{"User-Agent": "soap/0.1"},
map[string]string{
"User-Agent": "soap/0.1",
"SOAPAction": "SaveTrade",
},
},
// override default Content-Type
{
"SaveTrade",
map[string]string{"Content-Type": "text/xml; charset=\"utf-16\""},
map[string]string{"Content-Type": "text/xml; charset=\"utf-16\""},
},
}
var gotHeaders http.Header
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotHeaders = r.Header
}))
defer ts.Close()
for _, test := range tests {
client := NewClient(ts.URL, WithHTTPHeaders(test.reqHeaders))
req := struct{}{}
reply := struct{}{}
client.Call(test.action, req, reply)
for k, v := range test.expectedHeaders {
h := gotHeaders.Get(k)
if h != v {
t.Errorf("got %s wanted %s", h, v)
}
}
}
}
func TestClient_MTOM(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for k, v := range r.Header {
w.Header().Set(k, v[0])
}
bodyBuf, _ := ioutil.ReadAll(r.Body)
w.Write(bodyBuf)
}))
defer ts.Close()
client := NewClient(ts.URL, WithMTOM())
req := &PingRequest{Attachment: NewBinary([]byte("Attached data")).SetContentType("text/plain")}
reply := &PingRequest{}
if err := client.Call("GetData", req, reply); err != nil {
t.Fatalf("couln't call service: %v", err)
}
if !bytes.Equal(reply.Attachment.Bytes(), req.Attachment.Bytes()) {
t.Errorf("got %s wanted %s", reply.Attachment.Bytes(), req.Attachment.Bytes())
}
if reply.Attachment.ContentType() != req.Attachment.ContentType() {
t.Errorf("got %s wanted %s", reply.Attachment.Bytes(), req.Attachment.ContentType())
}
}