I am testing scenario where I want to inject a client in my “CreateEmployee” function, only for my test case and not in prod/stage
My handler func looks like…
// to create a employee in the system
func CreateEmployee(w http.ResponseWriter, r *http.Request) {
/// some code here, mapping json from request body to http request...etc
response, e := client.Do(request) // this is http.Client
}
I am writing a test case that looks like
func TestCreateEmployee(t *testing.T) {
var jsonStr = []byte(`{employee details as json}`)
req, err := http.NewRequest("POST", "http://www.exapmle.com/empsys/", bytes.NewBuffer(jsonStr))
resp := httptest.NewRecorder()
CreateEmployee(resp,req) // I am calling my function here, but not sure how client.Do will behave..
/// and further code
}
can anybody suggest a good way to test this scenario
Then you will have to catch this when you call client.Do. But since success of failure is not in your hands, your test will not be deterministic. This is not a good idea.
What are you trying to do? Testing code that depends on a third party service is not a good idea.
You could write a stub server for the third party service: Figure out the relevant parts of the HTTP API it provides and that you call will client.Do. Implement a HTTP service that stubs these parts of the API and that returns well defined data depending on known requests. Then write tests that make client.Do call the stub with these requests and test that the response is handled correctly by your code.
Again, what are you trying to test? What is CreateEmployee doing? There must be more in this function than just the request to the remote server. Your test code would test that, given a well defined request, this HTTP handler works correctly. What is it that CreateEmployee does before and after the remote call? That is what you want to test. The stub would help you to make the remote call work consistently.