gost-dom/browser

Support mocking external services

Open

#22 opened on Feb 1, 2025

 (0 comments) (0 reactions) (0 assignees)Go (15 forks)auto 404
good first issue

Repository metrics

Stars
 (295 stars)
PR merge metrics
 (PR metrics pending)

Description

Gost creates a custom http.Client to support calling an http handler directly bypassing the TCP/TLS layer.

It would be useful to extend, or create an alternate version, that can be configured with different hostnames mapping to different HTTP handlers.

A useful use case is replacing an identity provider, so the test outcome isn't dependent on an actual IDP running, as well as the overhead of communication with an external service. This also removes the need for test accounts in an external IDP.

An example: The web site has the following link:

<body>
  <a ref="http://idp.example.com/login?clientId=TEST_APP&redirectUrl=http://my-app.example.com/auth/oauthCallback">Login</a>
</body>

The IDL should obviously be configurable in the app, as well as it's own root url, when it generates links back to itself. For testing HTTP should be fine, but whether or not the links are actually HTTP or HTTPS probably shouldn't matter when the handler just calls a ServeHTTP function anyway.

An http client should support multiple hosts (hostname and port), and possibly allow unmatched hostnames to fallback to the default HTTP client, sending an outgoing request, which could be configured like this:

c := TestHttpClient{ // Find a better name perhaps? 
	AllowExternalRequests: true,
}
c.HostHandler("my-app.example.com", CreateMyAppRootHandler())
c.HostHandler("idp.example.com", CreateMockedIdentityProvider())
// or c.HostHandler("idp.example.com:8000", CreateMockedIdentityProvider())
b := browser.Browser{
  client: c.Client(),
}
win, _ := b.Open("http://my-app.example.com/")
win.Document().FindElementById("login-link").Click()
// go through login flow, and get back to own browser

Contributor guide