Skip to content

Commit

Permalink
Add logger tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vokomarov committed Dec 14, 2023
1 parent 4170078 commit ab6a0d7
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 6 deletions.
23 changes: 17 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import (
"github.com/cash-track/gateway/router"
)

const (
readBufferSize = 1024 * 8
writeBufferSize = 1024 * 8
)

func main() {
config.Global.Load()

Expand All @@ -25,25 +30,31 @@ func main() {
h = fasthttp.CompressHandler(h)
}

s := &fasthttp.Server{
Handler: h,
ReadBufferSize: readBufferSize,
WriteBufferSize: writeBufferSize,
}

if config.Global.HttpsEnabled {
startTls(h)
startTls(s)
} else {
start(h)
start(s)
}
}

func start(h fasthttp.RequestHandler) {
func start(s *fasthttp.Server) {
log.Printf("Listening on HTTP %s", config.Global.Address)

if err := fasthttp.ListenAndServe(config.Global.Address, h); err != nil {
if err := s.ListenAndServe(config.Global.Address); err != nil {
log.Fatalf("Error in HTTP server: %v", err)
}
}

func startTls(h fasthttp.RequestHandler) {
func startTls(s *fasthttp.Server) {
log.Printf("Listening on HTTPS %s", config.Global.Address)

if err := fasthttp.ListenAndServeTLS(config.Global.Address, config.Global.HttpsCrt, config.Global.HttpsKey, h); err != nil {
if err := s.ListenAndServeTLS(config.Global.Address, config.Global.HttpsCrt, config.Global.HttpsKey); err != nil {
log.Fatalf("Error in HTTPS server: %v", err)
}
}
43 changes: 43 additions & 0 deletions router/api/client/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package client

import (
"net/url"
"testing"

"github.com/stretchr/testify/assert"
"github.com/valyala/fasthttp"

"github.com/cash-track/gateway/config"
)

func TestNewClient(t *testing.T) {
NewClient()

assert.NotNil(t, client)
assert.True(t, client.NoDefaultUserAgentHeader)
assert.True(t, client.DisableHeaderNamesNormalizing)
assert.True(t, client.DisablePathNormalizing)
}

func TestSetRequestURI(t *testing.T) {
config.Global.ApiURI, _ = url.Parse("http://api.test.com")

uri := fasthttp.URI{}

setRequestURI(&uri, []byte("/users/create one"))

assert.Equal(t, "http://api.test.com/users/create%20one", uri.String())
}

func TestCopyRequestURI(t *testing.T) {
config.Global.ApiURI, _ = url.Parse("http://api.test.com")

src := fasthttp.URI{}
src.SetPath("/api/users/create one")
src.SetQueryString("one=two%203")
dest := fasthttp.URI{}

copyRequestURI(&src, &dest)

assert.Equal(t, "http://api.test.com/users/create%20one?one=two%203", dest.String())
}
42 changes: 42 additions & 0 deletions router/api/client/forward_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package client

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/valyala/fasthttp"

"github.com/cash-track/gateway/headers"
)

func TestForwardResponse(t *testing.T) {
ctx := fasthttp.RequestCtx{}

resp := fasthttp.Response{}
resp.SetStatusCode(fasthttp.StatusUnauthorized)
resp.SetBody([]byte("not allowed\n\r"))
resp.Header.Set(headers.AccessControlAllowOrigin, "test.com")
resp.Header.Set(headers.AccessControlMaxAge, "3600")
resp.Header.Set(headers.AccessControlAllowMethods, "GET,POST")
resp.Header.Set(headers.AccessControlAllowHeaders, "Content-Type,Accept-Language")
resp.Header.Set(headers.ContentType, "text/plain")
resp.Header.Set(headers.RetryAfter, "200")
resp.Header.Set(headers.Vary, "Content-Type,X-Rate-Limit")
resp.Header.Set(headers.XRateLimit, "123")
resp.Header.Set(headers.XRateLimitRemaining, "2")

err := ForwardResponse(&ctx, &resp)

assert.NoError(t, err)

assert.Equal(t, "test.com", string(ctx.Response.Header.Peek(headers.AccessControlAllowOrigin)))
assert.Equal(t, "true", string(ctx.Response.Header.Peek(headers.AccessControlAllowCredentials)))
assert.Equal(t, "GET,POST", string(ctx.Response.Header.Peek(headers.AccessControlAllowMethods)))
assert.Equal(t, "Content-Type,Accept-Language", string(ctx.Response.Header.Peek(headers.AccessControlAllowHeaders)))
assert.Equal(t, "3600", string(ctx.Response.Header.Peek(headers.AccessControlMaxAge)))
assert.Equal(t, "text/plain", string(ctx.Response.Header.Peek(headers.ContentType)))
assert.Equal(t, "200", string(ctx.Response.Header.Peek(headers.RetryAfter)))
assert.Equal(t, "Content-Type,X-Rate-Limit", string(ctx.Response.Header.Peek(headers.Vary)))
assert.Equal(t, "123", string(ctx.Response.Header.Peek(headers.XRateLimit)))
assert.Equal(t, "2", string(ctx.Response.Header.Peek(headers.XRateLimitRemaining)))
}
28 changes: 28 additions & 0 deletions router/router_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package router

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNew(t *testing.T) {
r := New()

l := r.List()

assert.Len(t, l, 2)

assert.NotNil(t, l["*"])
assert.Len(t, l["*"], 3)
assert.Contains(t, l["*"], "/live")
assert.Contains(t, l["*"], "/ready")
assert.Contains(t, l["*"], "/api/{path:*}")

assert.NotNil(t, l["POST"])
assert.Len(t, l["POST"], 4)
assert.Contains(t, l["POST"], "/api/auth/login")
assert.Contains(t, l["POST"], "/api/auth/logout")
assert.Contains(t, l["POST"], "/api/auth/register")
assert.Contains(t, l["POST"], "/api/auth/provider/google")
}

0 comments on commit ab6a0d7

Please sign in to comment.