| 1 | package soap |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "io/ioutil" |
| 6 | "net/http" |
| 7 | "net/url" |
| 8 | "reflect" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | type capturingRoundTripper struct { |
| 13 | err error |
| 14 | resp *http.Response |
| 15 | capturedReq *http.Request |
| 16 | } |
| 17 | |
| 18 | func (rt *capturingRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { |
| 19 | rt.capturedReq = req |
| 20 | return rt.resp, rt.err |
| 21 | } |
| 22 | |
| 23 | func TestActionInputs(t *testing.T) { |
| 24 | url, err := url.Parse("http://example.com/soap") |
| 25 | if err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | rt := &capturingRoundTripper{ |
| 29 | err: nil, |
| 30 | resp: &http.Response{ |
| 31 | StatusCode: 200, |
| 32 | Body: ioutil.NopCloser(bytes.NewBufferString(` |
| 33 | <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> |
| 34 | <s:Body> |
| 35 | <u:myactionResponse xmlns:u="mynamespace"> |
| 36 | <A>valueA</A> |
| 37 | <B>valueB</B> |
| 38 | </u:myactionResponse> |
| 39 | </s:Body> |
| 40 | </s:Envelope> |
| 41 | `)), |
| 42 | }, |
| 43 | } |
| 44 | client := SOAPClient{ |
| 45 | EndpointURL: *url, |
| 46 | HTTPClient: http.Client{ |
| 47 | Transport: rt, |
| 48 | }, |
| 49 | } |
| 50 | |
| 51 | type In struct { |
| 52 | Foo string |
| 53 | Bar string `soap:"bar"` |
| 54 | } |
| 55 | type Out struct { |
| 56 | A string |
| 57 | B string |
| 58 | } |
| 59 | in := In{"foo", "bar"} |
| 60 | gotOut := Out{} |
| 61 | err = client.PerformAction("mynamespace", "myaction", &in, &gotOut) |
| 62 | if err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | |
| 66 | wantBody := (soapPrefix + |
| 67 | `<u:myaction xmlns:u="mynamespace">` + |
| 68 | `<Foo>foo</Foo>` + |
| 69 | `<bar>bar</bar>` + |
| 70 | `</u:myaction>` + |
| 71 | soapSuffix) |
| 72 | body, err := ioutil.ReadAll(rt.capturedReq.Body) |
| 73 | if err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | gotBody := string(body) |
| 77 | if wantBody != gotBody { |
| 78 | t.Errorf("Bad request body\nwant: %q\n got: %q", wantBody, gotBody) |
| 79 | } |
| 80 | |
| 81 | wantOut := Out{"valueA", "valueB"} |
| 82 | if !reflect.DeepEqual(wantOut, gotOut) { |
| 83 | t.Errorf("Bad output\nwant: %+v\n got: %+v", wantOut, gotOut) |
| 84 | } |
| 85 | } |