lundi 6 juillet 2020

End to end testing Go app with subdomains - Mock subdomain

I have a Go app that use Gin gonic and a Nginx reverse proxy that send trafic to another app on domain.com and send all the *.domain.com subdomains traffic directly to my go app.pretty basic.

My Go app then has a middleware that will read the hostname that nginx passes to it from Context and allow my handlers to know what subdomain is being request and return the proper data and cookies for said subdomain.

It's a pretty simple setup and it seems to work fine from my test in postman as all my routes are the same across all my subdomains so this way i can only use one router for all of them instead of one router per subodmain.

Now my big problem come when i'm trying to do end to end testing.

I'm setting up my test like this :

  router := initRouter()
  w := httptest.NewRecorder()
  req, _ := http.NewRequest("POST", "/api/login", bytes.NewBuffer(jsonLogin))
  req.Header.Set("Content-Type", "application/json")
  router.ServeHTTP(w, req)
  assert.Equal(t, 200, w.Code)

with initRouter() returning a gin engine with all my routes and middlewares loaded and the rest as a basic test setup.

Obviously the test will fail as the gin Context won't ever receive a subdomain from context and act as if everything is coming from localhost:8000.

Is there a way to either :

  • "Mock" a subdomain so that the router think the call is coming from foo.localhost.com instead of localhost

  • Setup my test suit so that the test request are routed thought nginx.. i'd prefer solution 1 as this would be a mess to setup / maintain.

Aucun commentaire:

Enregistrer un commentaire