-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Michael Bretterklieber
committed
May 24, 2024
1 parent
3828e7f
commit 422c826
Showing
2 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package middleware | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"github.com/go-chi/chi/v5" | ||
"github.com/stretchr/testify/assert" | ||
"log/slog" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestHttpModemMiddleware(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
modem string | ||
expectStatusCode int | ||
}{ | ||
{ | ||
"Success", | ||
"1", | ||
http.StatusOK, | ||
}, | ||
{ | ||
"Success Empty", | ||
"", | ||
http.StatusOK, | ||
}, | ||
{ | ||
"Error invalid modem id", | ||
".*'", | ||
http.StatusBadRequest, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
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) | ||
|
||
chiCtx := chi.NewRouteContext() | ||
ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx) | ||
req = req.WithContext(ctx) | ||
|
||
rr := httptest.NewRecorder() | ||
|
||
mw := HttpModemMiddleware(logger) | ||
|
||
// 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) | ||
} | ||
|
||
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) | ||
} | ||
} |