|
| 1 | +package frontend |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/gin-gonic/gin" |
| 10 | + "github.com/user00265/dxclustergoapi/spot" |
| 11 | +) |
| 12 | + |
| 13 | +type mockCache struct { |
| 14 | + spots []spot.Spot |
| 15 | +} |
| 16 | + |
| 17 | +func (m *mockCache) GetAllSpots() []spot.Spot { |
| 18 | + return m.spots |
| 19 | +} |
| 20 | + |
| 21 | +func (m *mockCache) AddSpot(newSpot spot.Spot) bool { |
| 22 | + m.spots = append(m.spots, newSpot) |
| 23 | + return true |
| 24 | +} |
| 25 | + |
| 26 | +func setupTestRouter() *gin.Engine { |
| 27 | + gin.SetMode(gin.TestMode) |
| 28 | + router := gin.New() |
| 29 | + router.RedirectTrailingSlash = false |
| 30 | + router.RedirectFixedPath = false |
| 31 | + |
| 32 | + // Path normalization middleware |
| 33 | + router.Use(func(c *gin.Context) { |
| 34 | + path := c.Request.URL.Path |
| 35 | + originalPath := path |
| 36 | + |
| 37 | + // Collapse multiple slashes |
| 38 | + for strings.Contains(path, "//") { |
| 39 | + path = strings.ReplaceAll(path, "//", "/") |
| 40 | + } |
| 41 | + |
| 42 | + // Remove trailing slash (except for root) |
| 43 | + if len(path) > 1 && strings.HasSuffix(path, "/") { |
| 44 | + path = strings.TrimSuffix(path, "/") |
| 45 | + } |
| 46 | + |
| 47 | + // If path was modified, we need to re-route the request |
| 48 | + if path != originalPath { |
| 49 | + c.Request.URL.Path = path |
| 50 | + router.HandleContext(c) |
| 51 | + c.Abort() |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + c.Next() |
| 56 | + }) |
| 57 | + |
| 58 | + cache := &mockCache{ |
| 59 | + spots: []spot.Spot{ |
| 60 | + {Spotted: "TEST1", Band: "20m"}, |
| 61 | + {Spotted: "TEST2", Band: "40m"}, |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + SetupRoutes(router.Group("/"), cache, nil, nil) |
| 66 | + return router |
| 67 | +} |
| 68 | + |
| 69 | +func TestSpotsEndpointVariations(t *testing.T) { |
| 70 | + router := setupTestRouter() |
| 71 | + |
| 72 | + tests := []struct { |
| 73 | + name string |
| 74 | + path string |
| 75 | + wantStatus int |
| 76 | + }{ |
| 77 | + {"spots without slash", "/spots", http.StatusOK}, |
| 78 | + {"spots with trailing slash", "/spots/", http.StatusOK}, |
| 79 | + {"spots with double slash", "//spots", http.StatusOK}, |
| 80 | + {"spots with double slash and trailing", "//spots/", http.StatusOK}, |
| 81 | + {"spots with multiple slashes", "///spots", http.StatusOK}, |
| 82 | + {"spots with multiple slashes and trailing", "///spots/", http.StatusOK}, |
| 83 | + } |
| 84 | + |
| 85 | + for _, tt := range tests { |
| 86 | + t.Run(tt.name, func(t *testing.T) { |
| 87 | + req := httptest.NewRequest("GET", tt.path, nil) |
| 88 | + w := httptest.NewRecorder() |
| 89 | + router.ServeHTTP(w, req) |
| 90 | + |
| 91 | + if w.Code != tt.wantStatus { |
| 92 | + t.Errorf("GET %s returned status %d, want %d. Body: %s", |
| 93 | + tt.path, w.Code, tt.wantStatus, w.Body.String()) |
| 94 | + } |
| 95 | + |
| 96 | + // Verify no redirect |
| 97 | + if w.Code >= 300 && w.Code < 400 { |
| 98 | + t.Errorf("GET %s returned redirect %d, should return 200", tt.path, w.Code) |
| 99 | + } |
| 100 | + }) |
| 101 | + } |
| 102 | +} |
0 commit comments