Skip to content

Commit

Permalink
Refactoring: Created appbuilder for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Nov 21, 2023
1 parent ef0fdbf commit f6529c6
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 37 deletions.
65 changes: 28 additions & 37 deletions internal/uncors/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"testing"
"time"

"github.com/evg4b/uncors/testing/testutils/appbuilder"
"github.com/evg4b/uncors/internal/config"
"github.com/evg4b/uncors/internal/helpers"
"github.com/evg4b/uncors/internal/uncors"
"github.com/evg4b/uncors/testing/testutils"
"github.com/phayes/freeport"
"github.com/spf13/afero"
Expand All @@ -27,7 +27,10 @@ func TestUncorsApp(t *testing.T) {

t.Run("handle request", testutils.LogTest(func(t *testing.T, output *bytes.Buffer) {
t.Run("HTTP", func(t *testing.T) {
uncorsApp, uri := createApp(ctx, t, fs, false, &config.UncorsConfig{
appBuilder := appbuilder.NewAppBuilder(t).
WithFs(fs)

uncorsApp := appBuilder.Start(ctx, &config.UncorsConfig{
HTTPPort: freeport.GetPort(),
Mappings: config.Mappings{
config.Mapping{
Expand All @@ -42,13 +45,17 @@ func TestUncorsApp(t *testing.T) {
testutils.CheckNoServerError(t, err)
}()

response := makeRequest(t, http.DefaultClient, uri)
response := makeRequest(t, http.DefaultClient, appBuilder.URI())

assert.Equal(t, expectedResponse, response)
})

t.Run("HTTPS", testutils.WithTmpCerts(fs, func(t *testing.T, certs *testutils.Certs) {
uncorsApp, uri := createApp(ctx, t, fs, true, &config.UncorsConfig{
appBuilder := appbuilder.NewAppBuilder(t).
WithFs(fs).
WithHTTPS()

uncorsApp := appBuilder.Start(ctx, &config.UncorsConfig{
HTTPSPort: freeport.GetPort(),
CertFile: certs.CertPath,
KeyFile: certs.KeyPath,
Expand All @@ -71,7 +78,7 @@ func TestUncorsApp(t *testing.T) {
},
}

response := makeRequest(t, httpClient, uri)
response := makeRequest(t, httpClient, appBuilder.URI())

assert.Equal(t, expectedResponse, response)
}))
Expand All @@ -82,12 +89,15 @@ func TestUncorsApp(t *testing.T) {

t.Run("HTTP", func(t *testing.T) {
port := freeport.GetPort()
uncorsApp, uri := createApp(ctx, t, fs, false, &config.UncorsConfig{
appBuilder := appbuilder.NewAppBuilder(t).
WithFs(fs)

uncorsApp := appBuilder.Start(ctx, &config.UncorsConfig{
HTTPPort: port,
Mappings: config.Mappings{
config.Mapping{
From: "http://127.0.0.1",
To: "https://github.com",
From: hosts.Loopback.HTTP(),
To: hosts.Github.HTTPS(),
Mocks: mocks(expectedResponse),
},
},
Expand All @@ -97,7 +107,7 @@ func TestUncorsApp(t *testing.T) {
testutils.CheckNoServerError(t, err)
}()

response := makeRequest(t, http.DefaultClient, uri)
response := makeRequest(t, http.DefaultClient, appBuilder.URI())
assert.Equal(t, expectedResponse, response)

uncorsApp.Restart(ctx, &config.UncorsConfig{
Expand All @@ -113,14 +123,18 @@ func TestUncorsApp(t *testing.T) {

time.Sleep(delay)

response2 := makeRequest(t, http.DefaultClient, uri)
response2 := makeRequest(t, http.DefaultClient, appBuilder.URI())

assert.Equal(t, otherExpectedRepose, response2)
})

t.Run("HTTPS", testutils.WithTmpCerts(fs, func(t *testing.T, certs *testutils.Certs) {
port := freeport.GetPort()
uncorsApp, uri := createApp(ctx, t, fs, true, &config.UncorsConfig{
appBuilder := appbuilder.NewAppBuilder(t).
WithFs(fs).
WithHTTPS()

uncorsApp := appBuilder.Start(ctx, &config.UncorsConfig{
HTTPSPort: port,
CertFile: certs.CertPath,
KeyFile: certs.KeyPath,
Expand All @@ -143,7 +157,7 @@ func TestUncorsApp(t *testing.T) {
},
}

response := makeRequest(t, httpClient, uri)
response := makeRequest(t, httpClient, appBuilder.URI())

assert.Equal(t, expectedResponse, response)

Expand All @@ -162,14 +176,15 @@ func TestUncorsApp(t *testing.T) {

time.Sleep(delay)

response2 := makeRequest(t, httpClient, uri)
response2 := makeRequest(t, httpClient, appBuilder.URI())

assert.Equal(t, otherExpectedRepose, response2)
}))
}))
}

func makeRequest(t *testing.T, httpClient *http.Client, uri *url.URL) string {
t.Helper()
res, err := httpClient.Do(&http.Request{URL: uri, Method: http.MethodGet})
testutils.CheckNoError(t, err)
defer helpers.CloseSafe(res.Body)
Expand All @@ -180,30 +195,6 @@ func makeRequest(t *testing.T, httpClient *http.Client, uri *url.URL) string {
return string(data)
}

func createApp(
ctx context.Context,
t *testing.T, fs afero.Fs, https bool, config *config.UncorsConfig,
) (*uncors.App, *url.URL) {
app := uncors.CreateApp(fs, "x.x.x")

go app.Start(ctx, config)

time.Sleep(delay)

prefix := "http://"
if https {
prefix = "https://"
}
addr := app.HTTPAddr().String()
if https {
addr = app.HTTPSAddr().String()
}
uri, err := url.Parse(prefix + addr)
testutils.CheckNoError(t, err)

return app, uri
}

func mocks(response string) config.Mocks {
return config.Mocks{
config.Mock{
Expand Down
79 changes: 79 additions & 0 deletions testing/testutils/appbuilder/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package appbuilder

import (
"context"
"net/url"
"testing"
"time"

"github.com/evg4b/uncors/internal/config"
"github.com/evg4b/uncors/internal/uncors"
"github.com/evg4b/uncors/testing/testutils"
"github.com/spf13/afero"
)

const delay = 10 * time.Millisecond

type Builder struct {
t *testing.T
fs afero.Fs
uri *url.URL
https bool
}

func NewAppBuilder(t *testing.T) *Builder {
t.Helper()

return &Builder{t: t}
}

func (a *Builder) WithFs(fs afero.Fs) *Builder {
a.t.Helper()
a.fs = fs

return a
}

func (a *Builder) WithHTTPS() *Builder {
a.t.Helper()
a.https = true

return a
}

func (a *Builder) URI() *url.URL {
a.t.Helper()

return a.uri
}

func (a *Builder) Start(ctx context.Context, config *config.UncorsConfig) *uncors.App {
a.t.Helper()
app := uncors.CreateApp(a.fs, "x.x.x")
go app.Start(ctx, config)
time.Sleep(delay)
var err error
a.uri, err = url.Parse(a.prefix() + a.addr(app))
testutils.CheckNoError(a.t, err)

return app
}

func (a *Builder) addr(app *uncors.App) string {
a.t.Helper()
addr := app.HTTPAddr().String()
if a.https {
addr = app.HTTPSAddr().String()
}

return addr
}

func (a *Builder) prefix() string {
a.t.Helper()
if a.https {
return "https://"
}

return "http://"
}

0 comments on commit f6529c6

Please sign in to comment.