func TestEmptyHeader(t *testing.T) { h := Header{} b, err := xml.Marshal(h) if err != nil { t.Errorf("error: %s", err) return } expected := `<Header xmlns="http://schemas.xmlsoap.org/soap/envelope/"></Header>` actual := string(b) if expected != actual { t.Fatalf("expected: %s, actual: %s", expected, actual) } }
func TestEmptyEnvelope(t *testing.T) { env := Envelope{} b, err := xml.Marshal(env) if err != nil { t.Errorf("error: %s", err) return } expected := `<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"></Envelope>` actual := string(b) if expected != actual { t.Fatalf("expected: %s, actual: %s", expected, actual) } }
func (c *Client) RoundTrip(ctx context.Context, reqBody, resBody HasFault) error { var err error reqEnv := Envelope{Body: reqBody} resEnv := Envelope{Body: resBody} // Create debugging context for this round trip d := c.d.newRoundTrip() if d.enabled() { defer d.done() } b, err := xml.Marshal(reqEnv) if err != nil { panic(err) } rawReqBody := io.MultiReader(strings.NewReader(xml.Header), bytes.NewReader(b)) req, err := http.NewRequest("POST", c.u.String(), rawReqBody) if err != nil { panic(err) } req.Header.Set(`Content-Type`, `text/xml; charset="utf-8"`) soapAction := fmt.Sprintf("%s/%s", c.Namespace, c.Version) req.Header.Set(`SOAPAction`, soapAction) if c.UserAgent != "" { req.Header.Set(`User-Agent`, c.UserAgent) } if d.enabled() { d.debugRequest(req) } tstart := time.Now() res, err := c.do(ctx, req) tstop := time.Now() if d.enabled() { d.logf("%6dms (%T)", tstop.Sub(tstart)/time.Millisecond, resBody) } if err != nil { return err } if d.enabled() { d.debugResponse(res) } // Close response regardless of what happens next defer res.Body.Close() switch res.StatusCode { case http.StatusOK: // OK case http.StatusInternalServerError: // Error, but typically includes a body explaining the error default: return errors.New(res.Status) } dec := xml.NewDecoder(res.Body) dec.TypeFunc = types.TypeFunc() err = dec.Decode(&resEnv) if err != nil { return err } if f := resBody.Fault(); f != nil { return WrapSoapFault(f) } return err }