Skip to content

Commit 0ad0dae

Browse files
committed
Allow clients to add SOAPHeader in SOAPClient.
In order to allow the SOAPClient to send a SOAPHeader without modification of generated code, a new method SetHeader was added to both the SOAPClient and to the generated operation. The header, once set, is preserved between invocations of other methods in the same instance. This allows client code to call SetHeader() with the desired header, including nil to remove it from future calls in the same instance. The SOAPHeader usually holds authentication info, and we can benefit from reusing already set credentials between invocations.
1 parent a65655b commit 0ad0dae

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

gowsdl_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,19 @@ func TestVboxGeneratesWithoutSyntaxErrors(t *testing.T) {
6161
}
6262
}
6363
}
64+
65+
func TestSOAPHeaderGeneratesWithoutErrors(t *testing.T) {
66+
g := GoWSDL{
67+
file: "fixtures/ferry.wsdl",
68+
pkg: "myservice",
69+
makePublicFn: makePublic,
70+
}
71+
72+
resp, err := g.Start()
73+
if err != nil {
74+
t.Error(err)
75+
}
76+
if !strings.Contains(string(resp["operations"]), "SetHeader") {
77+
t.Error("SetHeader method should be generated in the service operation")
78+
}
79+
}

operations_tmpl.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ var opsTmpl = `
2222
}
2323
}
2424
25+
func (service *{{$portType}}) SetHeader(header interface{}) {
26+
service.client.SetHeader(header)
27+
}
28+
2529
{{range .Operations}}
2630
{{$faults := len .Faults}}
2731
{{$requestType := findType .Input.Message | replaceReservedWords | makePublic}}

soap_tmpl.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func dialTimeout(network, addr string) (net.Conn, error) {
1313
1414
type SOAPEnvelope struct {
1515
XMLName xml.Name ` + "`" + `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"` + "`" + `
16-
16+
Header *SOAPHeader
1717
Body SOAPBody
1818
}
1919
@@ -48,6 +48,7 @@ type SOAPClient struct {
4848
url string
4949
tls bool
5050
auth *BasicAuth
51+
header interface{}
5152
}
5253
5354
func (b *SOAPBody) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
@@ -112,9 +113,15 @@ func NewSOAPClient(url string, tls bool, auth *BasicAuth) *SOAPClient {
112113
}
113114
}
114115
116+
func (s *SOAPClient) SetHeader(header interface{}) {
117+
s.header = header
118+
}
119+
115120
func (s *SOAPClient) Call(soapAction string, request, response interface{}) error {
116-
envelope := SOAPEnvelope{
117-
//Header: SoapHeader{},
121+
envelope := SOAPEnvelope{}
122+
123+
if s.header != nil {
124+
envelope.Header = &SOAPHeader{Header: s.header}
118125
}
119126
120127
envelope.Body.Content = request

0 commit comments

Comments
 (0)