Skip to content

Commit

Permalink
Allow clients to add SOAPHeader in SOAPClient.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ronoaldo committed Jul 5, 2016
1 parent a65655b commit 0ad0dae
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
16 changes: 16 additions & 0 deletions gowsdl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,19 @@ func TestVboxGeneratesWithoutSyntaxErrors(t *testing.T) {
}
}
}

func TestSOAPHeaderGeneratesWithoutErrors(t *testing.T) {
g := GoWSDL{
file: "fixtures/ferry.wsdl",
pkg: "myservice",
makePublicFn: makePublic,
}

resp, err := g.Start()
if err != nil {
t.Error(err)
}
if !strings.Contains(string(resp["operations"]), "SetHeader") {
t.Error("SetHeader method should be generated in the service operation")
}
}
4 changes: 4 additions & 0 deletions operations_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ var opsTmpl = `
}
}
func (service *{{$portType}}) SetHeader(header interface{}) {
service.client.SetHeader(header)
}
{{range .Operations}}
{{$faults := len .Faults}}
{{$requestType := findType .Input.Message | replaceReservedWords | makePublic}}
Expand Down
13 changes: 10 additions & 3 deletions soap_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func dialTimeout(network, addr string) (net.Conn, error) {
type SOAPEnvelope struct {
XMLName xml.Name ` + "`" + `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"` + "`" + `
Header *SOAPHeader
Body SOAPBody
}
Expand Down Expand Up @@ -48,6 +48,7 @@ type SOAPClient struct {
url string
tls bool
auth *BasicAuth
header interface{}
}
func (b *SOAPBody) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
Expand Down Expand Up @@ -112,9 +113,15 @@ func NewSOAPClient(url string, tls bool, auth *BasicAuth) *SOAPClient {
}
}
func (s *SOAPClient) SetHeader(header interface{}) {
s.header = header
}
func (s *SOAPClient) Call(soapAction string, request, response interface{}) error {
envelope := SOAPEnvelope{
//Header: SoapHeader{},
envelope := SOAPEnvelope{}
if s.header != nil {
envelope.Header = &SOAPHeader{Header: s.header}
}
envelope.Body.Content = request
Expand Down

0 comments on commit 0ad0dae

Please sign in to comment.