Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the ability to set the Envelope Headers #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Client struct {
SoapVersion string
HTTPClientDoFn func(req *http.Request) (*http.Response, error)
LogRemoveHeaderNames []string
EnvelopeHeader *Header
}

// NewClient constructor. SOAP 1.1 is used by default. Switch to SOAP 1.2 with
Expand Down Expand Up @@ -96,6 +97,10 @@ func (c *Client) Call(ctx context.Context, soapAction string, request, response
Body: Body{Content: request},
}

if c.EnvelopeHeader != nil {
envelope.Header = *c.EnvelopeHeader
}

xmlBytes, err := c.Marshaller.Marshal(envelope)
if err != nil {
return nil, err
Expand Down
75 changes: 75 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ type FooRequest struct {
Foo string
}

type FooHeader struct {
XMLName xml.Name `xml:"fooHeader"`
Value string
}

// FooResponse a simple response
type FooResponse struct {
Bar string
Expand All @@ -34,6 +39,19 @@ func TestClient_Call(t *testing.T) {
</Body>
</Envelope>`)

wantSOAPBodyWithHeader := []byte(`<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<Header xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<fooHeader>
<Value>test header</Value>
</fooHeader>
</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">
Expand Down Expand Up @@ -80,7 +98,39 @@ func TestClient_Call(t *testing.T) {
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("with header success", func(t *testing.T) {
c := NewClient("http://localhorst.ch", &BasicAuth{
Login: "test",
Password: "test",
})
c.EnvelopeHeader = &Header{
Header: &FooHeader{Value: "test header"},
}
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, wantSOAPBodyWithHeader, 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) {
Expand Down Expand Up @@ -124,6 +174,31 @@ func TestClient_Call(t *testing.T) {
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("with header success", func(t *testing.T) {
c := NewClient("http://localhorst.ch", nil)
c.EnvelopeHeader = &Header{
Header: &FooHeader{Value: "test header"},
}
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) {
Expand Down