jarcoal / httpmock

HTTP mocking for Golang
http://godoc.org/github.com/jarcoal/httpmock
MIT License
1.93k stars 103 forks source link

Race condition due to ResponderFromResponse() copying Body object #93

Closed ascherkus closed 4 years ago

ascherkus commented 4 years ago

I came across this when parallelizing some backend HTTP calls.

The short of it is that NewStringResponder(), NewBytesResponder(), etc... are all subject to racing since ResponderFromResponse() will create new http.Response objects use the same underlying Body object in each response.

My assumption would be that supplying a string in NewStringResponder() would be free of races. If so - I believe a possible fix is to inline ResponderFromResponse() into each method and ensure NewRespBodyFromString() or NewRespBodyFromBytes() is called for each new http.Response object created.

maxatome commented 4 years ago

Thanks for the report @ascherkus could you check the PR #94 please?

ascherkus commented 4 years ago

tested locally and works for me!

maxatome commented 4 years ago

Fine, v1.0.6 released. Thanks for your help.