When using the local development server, the port for my default service always defaults to 8080. However, when I use aetest
, the port number always changes. The command used to start the local server when unit testing specifies --port=0
. However, since this is in the app engine package, I do not want to modify it. I also can't specify the port number manually since tests are run using go test
and not dev_appserver.py
.
What I need
The code I am testing requires a particular response from a different microservice to continue execution successfully. To keep testing to this microservice, I am trying to set up a fake endpoint to provide me with the response I need.
Sample of code being tested
func Sample(c *gin.Context) {
...
url := os.Getenv("some_service") + "/some-endpoint"
req, err := http.NewRequest("POST", url, r.Body)
if err != nil {
// error handling
}
...
}
I need the host of the current service being tested to be able to set dummy environment variables for another microservice it interacts with. The host url should be the url of the service currently undergoing tests. This way I can use factory responses since I am not testing the other services in this set of tests.
What I've Tried
appengine.DefaultVersionHostname()
, however this returns an empty stringQuestion
Is there any way to obtain the host for the local development server when running unit tests in GAE? Or, is there a way to specify the port number for app engine tests?
I was able to achieve my goal by first getting the list of modules. (It is likely the service name you are looking for will be default
, however I chose to use the first name in the list anyways). Using the module name, I got the default version for the module. Using both of these (and an empty instance name), I was able to get the module host name which provided the value I was looking for.
A code sample is shown below:
ml, - := module.List(ctx)
mv, _ := module.DefaultVersion(ctx, ml[0])
hn, _ := appengine.ModuleHostname(ctx, ml[0], mv, "")
Thanks to Dan for pointing me in the right direction.