Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection not closed properly #82

Open
pacbeckh opened this issue Aug 18, 2020 · 0 comments
Open

Connection not closed properly #82

pacbeckh opened this issue Aug 18, 2020 · 0 comments

Comments

@pacbeckh
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant