integrate context to struct and fix paths
This commit is contained in:
+109
-12
@@ -1,7 +1,6 @@
|
||||
package ron
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
@@ -83,7 +82,7 @@ func Test_applyEngineConfig(t *testing.T) {
|
||||
func Test_ServeHTTP(t *testing.T) {
|
||||
e := New()
|
||||
api := e.GROUP("/api")
|
||||
api.GET("/index", func(c *CTX, ctx context.Context) {
|
||||
api.GET("/index", func(c *CTX) {
|
||||
c.W.WriteHeader(http.StatusOK)
|
||||
c.W.Write([]byte("GET API"))
|
||||
})
|
||||
@@ -171,19 +170,19 @@ func Test_GET(t *testing.T) {
|
||||
{"resource with param", "GET", "/api/v1/resource/1", http.StatusOK, "GET Resource"},
|
||||
}
|
||||
|
||||
e.GET("/", func(c *CTX, ctx context.Context) {
|
||||
e.GET("/", func(c *CTX) {
|
||||
c.W.WriteHeader(http.StatusOK)
|
||||
c.W.Write([]byte("GET Root"))
|
||||
})
|
||||
e.GET("/api", func(c *CTX, ctx context.Context) {
|
||||
e.GET("/api", func(c *CTX) {
|
||||
c.W.WriteHeader(http.StatusOK)
|
||||
c.W.Write([]byte("GET API"))
|
||||
})
|
||||
e.GET("/api/v1", func(c *CTX, ctx context.Context) {
|
||||
e.GET("/api/v1", func(c *CTX) {
|
||||
c.W.WriteHeader(http.StatusOK)
|
||||
c.W.Write([]byte("GET API v1"))
|
||||
})
|
||||
e.GET("/api/v1/resource/{id}", func(c *CTX, ctx context.Context) {
|
||||
e.GET("/api/v1/resource/{id}", func(c *CTX) {
|
||||
c.W.WriteHeader(http.StatusOK)
|
||||
c.W.Write([]byte("GET Resource"))
|
||||
})
|
||||
@@ -207,7 +206,7 @@ func Test_GET(t *testing.T) {
|
||||
|
||||
func Test_POST(t *testing.T) {
|
||||
e := New()
|
||||
e.POST("/", func(c *CTX, ctx context.Context) {
|
||||
e.POST("/", func(c *CTX) {
|
||||
c.W.WriteHeader(http.StatusOK)
|
||||
c.W.Write([]byte("POST"))
|
||||
})
|
||||
@@ -225,10 +224,50 @@ func Test_POST(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func Test_PUT(t *testing.T) {
|
||||
e := New()
|
||||
e.PUT("/", func(c *CTX) {
|
||||
c.W.WriteHeader(http.StatusOK)
|
||||
c.W.Write([]byte("PUT"))
|
||||
})
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("PUT", "/", nil)
|
||||
e.ServeHTTP(rr, req)
|
||||
|
||||
if status := rr.Code; status != http.StatusOK {
|
||||
t.Errorf("Expected status code: %d, Actual: %d", http.StatusOK, status)
|
||||
}
|
||||
|
||||
if rr.Body.String() != "PUT" {
|
||||
t.Errorf("Expected: PUT, Actual: %s", rr.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func Test_DELETE(t *testing.T) {
|
||||
e := New()
|
||||
e.DELETE("/", func(c *CTX) {
|
||||
c.W.WriteHeader(http.StatusOK)
|
||||
c.W.Write([]byte("DELETE"))
|
||||
})
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("DELETE", "/", nil)
|
||||
e.ServeHTTP(rr, req)
|
||||
|
||||
if status := rr.Code; status != http.StatusOK {
|
||||
t.Errorf("Expected status code: %d, Actual: %d", http.StatusOK, status)
|
||||
}
|
||||
|
||||
if rr.Body.String() != "DELETE" {
|
||||
t.Errorf("Expected: DELETE, Actual: %s", rr.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func Test_GROUP(t *testing.T) {
|
||||
e := New()
|
||||
api := e.GROUP("/api")
|
||||
api.GET("/index", func(c *CTX, ctx context.Context) {
|
||||
api.GET("/index", func(c *CTX) {
|
||||
c.W.WriteHeader(http.StatusOK)
|
||||
c.W.Write([]byte("GET API"))
|
||||
})
|
||||
@@ -248,7 +287,7 @@ func Test_GROUP(t *testing.T) {
|
||||
|
||||
func Test_GROUPWithMiddleware(t *testing.T) {
|
||||
e := New()
|
||||
e.GET("/index", func(c *CTX, ctx context.Context) {
|
||||
e.GET("/index", func(c *CTX) {
|
||||
c.W.WriteHeader(http.StatusOK)
|
||||
c.W.Write([]byte("GET Root"))
|
||||
})
|
||||
@@ -266,7 +305,7 @@ func Test_GROUPWithMiddleware(t *testing.T) {
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
})
|
||||
api.GET("/index", func(c *CTX, ctx context.Context) {
|
||||
api.GET("/index", func(c *CTX) {
|
||||
c.W.WriteHeader(http.StatusOK)
|
||||
c.W.Write([]byte("GET API"))
|
||||
})
|
||||
@@ -287,7 +326,7 @@ func Test_GROUPWithMiddleware(t *testing.T) {
|
||||
func Test_GROUPPOST(t *testing.T) {
|
||||
e := New()
|
||||
api := e.GROUP("/api")
|
||||
api.POST("/index", func(c *CTX, ctx context.Context) {
|
||||
api.POST("/index", func(c *CTX) {
|
||||
c.W.WriteHeader(http.StatusOK)
|
||||
c.W.Write([]byte("POST API"))
|
||||
})
|
||||
@@ -305,6 +344,48 @@ func Test_GROUPPOST(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func Test_GROUPPUT(t *testing.T) {
|
||||
e := New()
|
||||
api := e.GROUP("/api")
|
||||
api.PUT("/index", func(c *CTX) {
|
||||
c.W.WriteHeader(http.StatusOK)
|
||||
c.W.Write([]byte("PUT API"))
|
||||
})
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("PUT", "/api/index", nil)
|
||||
e.ServeHTTP(rr, req)
|
||||
|
||||
if status := rr.Code; status != http.StatusOK {
|
||||
t.Errorf("Expected status code: %d, Actual: %d", http.StatusOK, status)
|
||||
}
|
||||
|
||||
if rr.Body.String() != "PUT API" {
|
||||
t.Errorf("Expected: PUT API, Actual: %s", rr.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func Test_GROUPDELETE(t *testing.T) {
|
||||
e := New()
|
||||
api := e.GROUP("/api")
|
||||
api.DELETE("/index", func(c *CTX) {
|
||||
c.W.WriteHeader(http.StatusOK)
|
||||
c.W.Write([]byte("DELETE API"))
|
||||
})
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("DELETE", "/api/index", nil)
|
||||
e.ServeHTTP(rr, req)
|
||||
|
||||
if status := rr.Code; status != http.StatusOK {
|
||||
t.Errorf("Expected status code: %d, Actual: %d", http.StatusOK, status)
|
||||
}
|
||||
|
||||
if rr.Body.String() != "DELETE API" {
|
||||
t.Errorf("Expected: DELETE API, Actual: %s", rr.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Static(t *testing.T) {
|
||||
tests := map[string]struct {
|
||||
givenPath string
|
||||
@@ -328,7 +409,7 @@ func Test_Static(t *testing.T) {
|
||||
givenDirectory: "assets",
|
||||
expectedResponse: testhelpers.ExpectedResponse{
|
||||
Code: http.StatusOK,
|
||||
Header: HeaderAppJS,
|
||||
Header: HeaderAppJS_UTF8,
|
||||
Body: "console.log('Hello, World!');",
|
||||
},
|
||||
},
|
||||
@@ -486,3 +567,19 @@ func Test_newLogger(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Benchmark_GET(b *testing.B) {
|
||||
engine := New()
|
||||
|
||||
engine.GET("/hello", func(c *CTX) {
|
||||
c.W.Write([]byte("Hello"))
|
||||
})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/hello", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
engine.ServeHTTP(w, req)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user