mirror of
https://github.com/weloe/token-go.git
synced 2025-10-06 07:56:58 +08:00
feat: add adapter
This commit is contained in:
@@ -2,6 +2,7 @@ package go_http_context
|
||||
|
||||
import (
|
||||
"github.com/weloe/token-go/ctx"
|
||||
"net/http"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
@@ -13,6 +14,14 @@ type HttpContext struct {
|
||||
reqStorage ctx.ReqStorage
|
||||
}
|
||||
|
||||
func NewHttpContext(req *http.Request, writer http.ResponseWriter) *HttpContext {
|
||||
return &HttpContext{
|
||||
req: NewHttpRequest(req),
|
||||
response: NewResponse(req, writer),
|
||||
reqStorage: NewReqStorage(req),
|
||||
}
|
||||
}
|
||||
|
||||
func (h *HttpContext) IsValidContext() bool {
|
||||
return h.req != nil && !reflect.DeepEqual(h.req, &HttpRequest{})
|
||||
}
|
||||
|
@@ -48,7 +48,11 @@ func NewTestHttpRequest(t *testing.T) *HttpRequest {
|
||||
|
||||
func NewTestHttpReqStore(t *testing.T) *HttpReqStorage {
|
||||
request := NewTestRequest(t)
|
||||
httpReqStorage := NewReqStorage(request)
|
||||
httpReqStorage := NewReqStorage(nil)
|
||||
if httpReqStorage != nil {
|
||||
t.Errorf("NewReqStorage() failed: value = %v", httpReqStorage)
|
||||
}
|
||||
httpReqStorage = NewReqStorage(request)
|
||||
return httpReqStorage
|
||||
}
|
||||
|
||||
@@ -441,3 +445,16 @@ func TestDeleteCookieHandler(t *testing.T) {
|
||||
func containsString(s string, substr string) bool {
|
||||
return len(s) >= len(substr) && s[:len(substr)] == substr
|
||||
}
|
||||
|
||||
func TestNewHttpContext(t *testing.T) {
|
||||
context := NewHttpContext(nil, nil)
|
||||
if context == nil {
|
||||
t.Errorf("NewHttpContext() failed: value = %v", context)
|
||||
}
|
||||
request := context.Request()
|
||||
response := context.Response()
|
||||
storage := context.ReqStorage()
|
||||
if request == nil || response == nil || storage == nil {
|
||||
t.Errorf("HttpContext failed ")
|
||||
}
|
||||
}
|
||||
|
@@ -11,6 +11,9 @@ type HttpReqStorage struct {
|
||||
}
|
||||
|
||||
func NewReqStorage(req *http.Request) *HttpReqStorage {
|
||||
if req == nil {
|
||||
return nil
|
||||
}
|
||||
return &HttpReqStorage{source: req.Context()}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user