Skip to content

Commit

Permalink
fix table test run
Browse files Browse the repository at this point in the history
  • Loading branch information
mbretter committed Jun 1, 2024
1 parent e827dee commit 48d2525
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 23 deletions.
33 changes: 33 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"github.com/go-chi/chi/v5"
"github.com/mbretter/go-mmcli-svr/backend"
"net/http"
"testing"
)

func TestRegisterSmsRoutes(t *testing.T) {
backendMock := backend.NewBackendMock(t)

tests := []struct {
name string
method string
path string
expectedStatus int
}{
{"Get All Sms", http.MethodGet, "/sms/", http.StatusOK},
{"Get Sms By ID", http.MethodGet, "/sms/1", http.StatusOK},
{"Send Sms", http.MethodPost, "/sms", http.StatusOK},
{"Delete Sms", http.MethodDelete, "/sms/1", http.StatusOK},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := chi.NewRouter()
registerSmsRoutes(r, backendMock)

r.Routes()
})
}
}
48 changes: 25 additions & 23 deletions middleware/modem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,38 @@ func TestHttpModemMiddleware(t *testing.T) {
}

for _, tt := range tests {
var buff bytes.Buffer
logger := slog.New(slog.NewTextHandler(&buff, nil))
t.Run(tt.name, func(t *testing.T) {
var buff bytes.Buffer
logger := slog.New(slog.NewTextHandler(&buff, nil))

req := httptest.NewRequest("GET", "http://localhost/?modem="+tt.modem, nil)
ctx := context.WithValue(req.Context(), "logger", logger)
req := httptest.NewRequest("GET", "http://localhost/?modem="+tt.modem, nil)
ctx := context.WithValue(req.Context(), "logger", logger)

chiCtx := chi.NewRouteContext()
ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
req = req.WithContext(ctx)
chiCtx := chi.NewRouteContext()
ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
req = req.WithContext(ctx)

rr := httptest.NewRecorder()
rr := httptest.NewRecorder()

mw := HttpModemMiddleware(logger)
mw := HttpModemMiddleware(logger)

// Run the middleware function
mw(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
modem, ok := r.Context().Value("modem").(string)
// Run the middleware function
mw(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
modem, ok := r.Context().Value("modem").(string)

if tt.expectStatusCode == http.StatusOK {
if tt.modem != "" {
assert.Contains(t, buff.String(), "level=INFO msg=HttpModemMiddleware modem="+tt.modem)
}
if tt.expectStatusCode == http.StatusOK {
if tt.modem != "" {
assert.Contains(t, buff.String(), "level=INFO msg=HttpModemMiddleware modem="+tt.modem)
}

assert.Equal(t, modem, tt.modem)
assert.True(t, ok)
} else {
assert.False(t, ok)
}
})).ServeHTTP(rr, req)
assert.Equal(t, modem, tt.modem)
assert.True(t, ok)
} else {
assert.False(t, ok)
}
})).ServeHTTP(rr, req)

assert.Equal(t, tt.expectStatusCode, rr.Code)
assert.Equal(t, tt.expectStatusCode, rr.Code)
})
}
}

0 comments on commit 48d2525

Please sign in to comment.