Skip to content

Commit

Permalink
chore: update test
Browse files Browse the repository at this point in the history
  • Loading branch information
pejeio committed Feb 13, 2024
1 parent 6a1769c commit 4d47114
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 12 deletions.
23 changes: 11 additions & 12 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,23 @@ name: Go

on:
push:
branches: [ "main" ]
branches: ["main"]
pull_request:
branches: [ "main" ]
branches: ["main"]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "1.20"

- name: Build
run: go build -v ./...
- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
- name: Test
run: go test -v ./...
5 changes: 5 additions & 0 deletions internal/api/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ type JSONErrorResponse struct {
Message string `json:"message"`
}

// GetBearerTokenFromHeaders retrieves the bearer token from the headers of a fiber.Ctx object.
func GetBearerTokenFromHeaders(c *fiber.Ctx) string {
if c == nil {
return ""
}

authHeader := c.Get("Authorization")
if authHeader == "" {
return ""
Expand Down
52 changes: 52 additions & 0 deletions internal/api/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package api

import (
"testing"

"github.com/gofiber/fiber/v2"
"github.com/valyala/fasthttp"
)

func TestGetBearerTokenFromHeaders(t *testing.T) {
t.Parallel()
app := fiber.New()

// Test with nil context
t.Run("Nil Context", func(t *testing.T) {
token := GetBearerTokenFromHeaders(nil)
if token != "" {
t.Errorf("Expected empty token for nil context, but got %s", token)
}
})

// Test with missing Authorization header
t.Run("Missing Authorization Header", func(t *testing.T) {
ctx := app.AcquireCtx(&fasthttp.RequestCtx{})
token := GetBearerTokenFromHeaders(ctx)
if token != "" {
t.Errorf("Expected empty token for missing Authorization header, but got %s", token)
}
})

// Test with improperly formatted Authorization header
t.Run("Improperly Formatted Authorization Header", func(t *testing.T) {
ctx := app.AcquireCtx(&fasthttp.RequestCtx{})
// ctx.Set("Authorization", "InvalidHeaderFormat")
ctx.Request().Header.Set("Authorization", "InvalidHeaderFormat")
token := GetBearerTokenFromHeaders(ctx)
if token != "" {
t.Errorf("Expected empty token for improperly formatted Authorization header, but got %s", token)
}
})

// Test with valid Authorization header
t.Run("Valid Authorization Header", func(t *testing.T) {
ctx := app.AcquireCtx(&fasthttp.RequestCtx{})
ctx.Request().Header.Set("Authorization", "Bearer YourValidTokenHere")
token := GetBearerTokenFromHeaders(ctx)
expectedToken := "YourValidTokenHere"
if token != expectedToken {
t.Errorf("Expected token: %s, but got: %s", expectedToken, token)
}
})
}

0 comments on commit 4d47114

Please sign in to comment.