Skip to content

Commit

Permalink
Updates: Improve test coverage env (#21)
Browse files Browse the repository at this point in the history
* Set & Get value from context methods

* middleware, handle methods tests
  • Loading branch information
2hmad authored Apr 21, 2023
1 parent b8efe09 commit 6ee791c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 37 deletions.
13 changes: 7 additions & 6 deletions context.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pulse

import (
"context"
"encoding/json"
"net/http"
"strings"
Expand Down Expand Up @@ -72,14 +73,14 @@ func (c *Context) String(value string) {
}
}

// SetData sets the http header value to the given key.
func (c *Context) SetData(key string, value interface{}) {
c.SetResponseHeader(key, value.(string))
// SetValue create a middleware that adds a value to the context
func (c *Context) SetValue(key interface{}, value interface{}) {
c.Request = c.Request.WithContext(context.WithValue(c.Request.Context(), key, value))
}

// GetData returns the http header value for the given key.
func (c *Context) GetData(key string) string {
return string(c.Request.Header.Get(key))
// GetValue returns the value for the given key.
func (c *Context) GetValue(key string) string {
return c.Request.Context().Value(key).(string)
}

// Next calls the next handler in the chain.
Expand Down
38 changes: 8 additions & 30 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,39 +165,14 @@ func TestContext_Header(t *testing.T) {
}
}

func TestContext_SetData(t *testing.T) {
w := httptest.NewRecorder()
ctx := NewContext(w, nil)

key := "custom-key"
value := "custom-value"

ctx.SetData(key, value)

if w.Header().Get(key) != value {
t.Errorf("Response header does not match expected value. Expected: %s, got: %s", value, w.Header().Get(key))
}
}

func TestContext_GetData(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, "/", nil)
r.Header.Set("custom-key", "custom-value")

w := httptest.NewRecorder()
ctx := NewContext(w, r)

key := "custom-key"

if ctx.GetData(key) != "custom-value" {
t.Errorf("Request header does not match expected value. Expected: custom-value, got: %s", ctx.GetData(key))
}
}

func TestContext_Next(t *testing.T) {
w := httptest.NewRecorder()
ctx := NewContext(w, nil)

ctx.Next()
err := ctx.Next()
if err != nil {
return
}
}

func TestContext_Reset(t *testing.T) {
Expand All @@ -224,7 +199,10 @@ func TestContext_JSON(t *testing.T) {
w := httptest.NewRecorder()
ctx := NewContext(w, nil)

ctx.JSON(200, map[string]string{"test": "test"})
_, err := ctx.JSON(200, map[string]string{"test": "test"})
if err != nil {
return
}
}

func TestContext_SetContentType(t *testing.T) {
Expand Down
67 changes: 66 additions & 1 deletion middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,41 @@ import (
"testing"
)

func TestRouter_Use(t *testing.T) {
func TestMiddlewareFunc_Middleware(t *testing.T) {
// create a mock handler
mockHandler := func(ctx *Context) error { return nil }

// create a middleware that adds a value to the context
middleware := MiddlewareFunc(func(handler Handler) Handler {
return func(ctx *Context) error {
ctx.SetValue("key", "value")
return handler(ctx)
}
})

// call the Middleware method with the mock handler
newHandler := middleware.Middleware(mockHandler)

// create a new context instance
req := httptest.NewRequest(http.MethodGet, "/test", nil)
w := httptest.NewRecorder()
ctx := NewContext(w, req)

// call the new handler with the context
err := newHandler(ctx)

// check if the context value was set correctly
if val := ctx.GetValue("key"); val != "value" {
t.Errorf("Expected context value for key \"key\" to be \"value\", but got %v", val)
}

// check if the original handler was called with the context
if err != nil {
t.Errorf("Expected err to be nil, but got %v", err)
}
}

func TestMiddleware_Use(t *testing.T) {
// Create a new router.
r := NewRouter()

Expand Down Expand Up @@ -75,3 +109,34 @@ func TestCORSMiddleware(t *testing.T) {
t.Errorf("Expected handler to return no error, but got %v", err)
}
}

func TestMiddlewareFunc_Handle(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/test", nil)
w := httptest.NewRecorder()

ctx := NewContext(w, req)

// create a mock handler
mockHandler := func(ctx *Context) error { return nil }

// create a middleware that adds a value to the context
middleware := MiddlewareFunc(func(handler Handler) Handler {
return func(ctx *Context) error {
ctx.SetValue("key", "value")
return handler(ctx)
}
})

// call the Handle method with the mock handler as the next handler
err := middleware.Handle(ctx, mockHandler)

// check if the context value was set correctly
if val := ctx.GetValue("key"); val != "value" {
t.Errorf("Expected context value for key \"key\" to be \"value\", but got %v", val)
}

// check if the next handler was called with the original context
if err != nil {
t.Errorf("Expected err to be nil, but got %v", err)
}
}

0 comments on commit 6ee791c

Please sign in to comment.