openmock
openmock copied to clipboard
Connection not closed properly
We're using go's "net/http" client to send http requests to the stub. When sending multiple POST requests to the openmock http server the client fails with an EOF error.
After investigating further we noticed that the this error occurs if the server closed the connection without noticing the client. https://stackoverflow.com/questions/28046100/golang-http-concurrent-requests-post-eof/34474535#34474535
To reproduce this I created a simple stub:
- key: mystub
kind: Behavior
expect:
http:
method: POST
path: /
actions:
- reply_http:
status_code: 200
body: somepostresponse
And a small go test
import (
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"testing"
)
func TestPost(t *testing.T) {
for i := 0; i < 20; i++ {
if err := sendRequest(i); err != nil {
panic(fmt.Sprintf("sending request %d failed because of %v", i, err))
}
}
}
func sendRequest(i int) error {
resp, err := http.Post("http://localhost:9091", "text", strings.NewReader("request "+strconv.Itoa(i)))
if err != nil {
panic(err)
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
println(fmt.Sprintf("got response %s for request %d", string(respBody), i))
return nil
}
Our current workaround is to add the "connection: close" header to each stubbed request to signal the http client that it shouldn't reuse the connection, but is there any way this can be solved in the openmock server? If not, then this at least might be useful for the next person using go and running into this issue.
Thanks!