Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@nij4t
Copy link
Contributor

@nij4t nij4t commented Apr 9, 2020

The following PR is aimed to provide a way of testing the library's components without the need of a remote connection. This way the SOAPClient can be created with a custom RoundTripper injected to http.Client which can be used during the unit tests.

// Defining custom round tripper
type RoundTripFunc func(req *http.Request) *http.Response

func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
	return f(req), nil
}

// NewTestClient returns *http.Client
// with Transport replaced to avoid making real calls
func NewTestClient(fn RoundTripFunc) *http.Client {
	return &http.Client{
		Transport: RoundTripFunc(fn),
	}
}

// Creating httpClient with mocked response
fakeclient := NewTestClient(func(req *http.Request) *http.Response {
	return &http.Response{
		StatusCode: 200,
		Body:       ioutil.NopCloser(bytes.NewStringBuffer("Fake document")),
		Header:     make(http.Header),
	}
})

// When sending requests via our client 
// we will get the same response no matter
// what our request object is
res, _ := fakeclient.Get("http://www.example.com")
println(string(res.Body))
// Output: Fake document

@tiaguinho
Copy link
Owner

Hi @nij4t can you fix the conflicts?

@tiaguinho tiaguinho merged commit 8cc3c3e into tiaguinho:master Jun 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants