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

Set the IP address of the outgoing test request #59

Open
apratheek opened this issue Mar 8, 2018 · 1 comment
Open

Set the IP address of the outgoing test request #59

apratheek opened this issue Mar 8, 2018 · 1 comment

Comments

@apratheek
Copy link

Hi, is there any way to set the IP address of the mock request? I have a use case where I need to know the IP address of the incoming request. The library sets the IP address to an empty string. I need to set it to a particular value (could default to 127.0.0.1). Any help would be much appreciated.

Thanks

@KantarBruceAdams
Copy link

KantarBruceAdams commented Jul 16, 2019

I've been doing this by setting a fake server address directly after creating the engine:

e := NewEngine()
e.Server.Addr = FakeServerAddress

Rather than using c.Request().Host directly this must be matched on the server side with something like:

// get the IP address of the running server
func GetServerAddress(c echo.Context) string {
	serverAddress := c.Request().Host
	if serverAddress == "" {
		// normally we use the address given in the HTTP request
		// this is not set by gofight in unit tests so we use the listener address instead
		if c.Echo().Server != nil {
			serverAddress = c.Echo().Server.Addr
		} else if c.Echo().Listener != nil {
			serverAddress = c.Echo().Listener.Addr().String()
		}
	}
	return serverAddress
}

as this could be a different IP & port. For example, if your server is running inside a docker container.

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

No branches or pull requests

2 participants