mirror of
https://github.com/weloe/token-go.git
synced 2025-10-06 16:07:18 +08:00
feat: add web context and add go net/http web context
This commit is contained in:
2
.github/workflows/go.yml
vendored
2
.github/workflows/go.yml
vendored
@@ -44,7 +44,7 @@ jobs:
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v2
|
||||
with:
|
||||
go-version: '1.18'
|
||||
go-version: '1.20'
|
||||
|
||||
- name: golangci-lint
|
||||
uses: golangci/golangci-lint-action@v3
|
||||
|
9
constant/constant.go
Normal file
9
constant/constant.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package constant
|
||||
|
||||
// ctx.Response constant variable
|
||||
const (
|
||||
AccessControlExposeHeaders = "Access-Control-Expose-Headers"
|
||||
|
||||
SetCookie = "Set-Cookie"
|
||||
)
|
||||
|
9
ctx/context.go
Normal file
9
ctx/context.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package ctx
|
||||
|
||||
type Context interface {
|
||||
Request() Request
|
||||
Response() Response
|
||||
ReqStorage() ReqStorage
|
||||
MatchPath(pattern string, path string) bool
|
||||
IsValidContext() bool
|
||||
}
|
1
ctx/cookie.go
Normal file
1
ctx/cookie.go
Normal file
@@ -0,0 +1 @@
|
||||
package ctx
|
51
ctx/default_response.go
Normal file
51
ctx/default_response.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package ctx
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/weloe/token-go/constant"
|
||||
"time"
|
||||
)
|
||||
|
||||
var _ Response = (*DefaultRespImplement)(nil)
|
||||
|
||||
type DefaultRespImplement struct {
|
||||
}
|
||||
|
||||
func (r *DefaultRespImplement) Source() interface{} {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (r *DefaultRespImplement) SetHeader(name string, value string) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (r *DefaultRespImplement) AddHeader(name string, value string) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (r *DefaultRespImplement) Redirect(url string) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (r *DefaultRespImplement) Status(status int) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (r *DefaultRespImplement) DeleteCookie(name string, path string, domain string) {
|
||||
r.AddCookie(name, "", path, domain, 0)
|
||||
}
|
||||
|
||||
func (r *DefaultRespImplement) AddCookie(name string, value string, path string, domain string, timeout int64) {
|
||||
cookie := fmt.Sprintf("%s=%s; Path=%s; Domain=%s; Expires=%s",
|
||||
name,
|
||||
value,
|
||||
path,
|
||||
domain,
|
||||
time.Now().Add(time.Second*time.Duration(timeout)).Format(time.RFC1123),
|
||||
)
|
||||
r.AddHeader(constant.SetCookie, cookie)
|
||||
}
|
||||
|
||||
func (r *DefaultRespImplement) SetServer(value string) {
|
||||
r.SetHeader("Server", value)
|
||||
}
|
34
ctx/go-http-context/context.go
Normal file
34
ctx/go-http-context/context.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package go_http_context
|
||||
|
||||
import (
|
||||
"github.com/weloe/token-go/ctx"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
var _ ctx.Context = (*HttpContext)(nil)
|
||||
|
||||
type HttpContext struct {
|
||||
req ctx.Request
|
||||
response ctx.Response
|
||||
reqStorage ctx.ReqStorage
|
||||
}
|
||||
|
||||
func (h *HttpContext) IsValidContext() bool {
|
||||
return h.req != nil && !reflect.DeepEqual(h.req, &HttpRequest{})
|
||||
}
|
||||
|
||||
func (h *HttpContext) Request() ctx.Request {
|
||||
return h.req
|
||||
}
|
||||
|
||||
func (h *HttpContext) ReqStorage() ctx.ReqStorage {
|
||||
return h.reqStorage
|
||||
}
|
||||
|
||||
func (h *HttpContext) Response() ctx.Response {
|
||||
return h.response
|
||||
}
|
||||
|
||||
func (h *HttpContext) MatchPath(pattern string, path string) bool {
|
||||
return true
|
||||
}
|
647
ctx/go-http-context/context_test.go
Normal file
647
ctx/go-http-context/context_test.go
Normal file
@@ -0,0 +1,647 @@
|
||||
package go_http_context
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/weloe/token-go/ctx"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHttpContext_IsValidContext(t *testing.T) {
|
||||
type fields struct {
|
||||
req ctx.Request
|
||||
response ctx.Response
|
||||
reqStorage ctx.ReqStorage
|
||||
}
|
||||
request, err := http.NewRequest("GET", "https://baidu.com", strings.NewReader(""))
|
||||
if err != nil {
|
||||
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "1",
|
||||
fields: fields{
|
||||
req: nil,
|
||||
response: nil,
|
||||
reqStorage: nil,
|
||||
},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "2",
|
||||
fields: fields{
|
||||
req: &HttpRequest{},
|
||||
response: nil,
|
||||
reqStorage: nil,
|
||||
},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "3",
|
||||
fields: fields{
|
||||
req: &HttpRequest{source: &http.Request{}},
|
||||
response: nil,
|
||||
reqStorage: nil,
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "4",
|
||||
fields: fields{
|
||||
req: &HttpRequest{source: request},
|
||||
response: nil,
|
||||
reqStorage: nil,
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
h := &HttpContext{
|
||||
req: tt.fields.req,
|
||||
response: tt.fields.response,
|
||||
reqStorage: tt.fields.reqStorage,
|
||||
}
|
||||
if got := h.IsValidContext(); got != tt.want {
|
||||
t.Errorf("IsValidContext() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpContext_MatchPath(t *testing.T) {
|
||||
type fields struct {
|
||||
req ctx.Request
|
||||
response ctx.Response
|
||||
reqStorage ctx.ReqStorage
|
||||
}
|
||||
type args struct {
|
||||
pattern string
|
||||
path string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
want bool
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
h := &HttpContext{
|
||||
req: tt.fields.req,
|
||||
response: tt.fields.response,
|
||||
reqStorage: tt.fields.reqStorage,
|
||||
}
|
||||
if got := h.MatchPath(tt.args.pattern, tt.args.path); got != tt.want {
|
||||
t.Errorf("MatchPath() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpReqStorage_Delete(t *testing.T) {
|
||||
type fields struct {
|
||||
source context.Context
|
||||
}
|
||||
type args struct {
|
||||
key string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
r := HttpReqStorage{
|
||||
source: tt.fields.source,
|
||||
}
|
||||
r.Delete(tt.args.key)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpReqStorage_Get(t *testing.T) {
|
||||
type fields struct {
|
||||
source context.Context
|
||||
}
|
||||
type args struct {
|
||||
key string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
want interface{}
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
r := HttpReqStorage{
|
||||
source: tt.fields.source,
|
||||
}
|
||||
if got := r.Get(tt.args.key); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("Get() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpReqStorage_Set(t *testing.T) {
|
||||
type fields struct {
|
||||
source context.Context
|
||||
}
|
||||
type args struct {
|
||||
key string
|
||||
value string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
r := HttpReqStorage{
|
||||
source: tt.fields.source,
|
||||
}
|
||||
r.Set(tt.args.key, tt.args.value)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpRequest_Cookie(t *testing.T) {
|
||||
type fields struct {
|
||||
source *http.Request
|
||||
}
|
||||
type args struct {
|
||||
key string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
want string
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
d := &HttpRequest{
|
||||
source: tt.fields.source,
|
||||
}
|
||||
if got := d.Cookie(tt.args.key); got != tt.want {
|
||||
t.Errorf("Cookie() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpRequest_Header(t *testing.T) {
|
||||
type fields struct {
|
||||
source *http.Request
|
||||
}
|
||||
type args struct {
|
||||
key string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
want string
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
d := &HttpRequest{
|
||||
source: tt.fields.source,
|
||||
}
|
||||
if got := d.Header(tt.args.key); got != tt.want {
|
||||
t.Errorf("Header() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpRequest_Method(t *testing.T) {
|
||||
type fields struct {
|
||||
source *http.Request
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
want string
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
d := &HttpRequest{
|
||||
source: tt.fields.source,
|
||||
}
|
||||
if got := d.Method(); got != tt.want {
|
||||
t.Errorf("Method() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpRequest_Path(t *testing.T) {
|
||||
type fields struct {
|
||||
source *http.Request
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
want string
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
d := &HttpRequest{
|
||||
source: tt.fields.source,
|
||||
}
|
||||
if got := d.Path(); got != tt.want {
|
||||
t.Errorf("Path() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpRequest_PostForm(t *testing.T) {
|
||||
type fields struct {
|
||||
source *http.Request
|
||||
}
|
||||
type args struct {
|
||||
key string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
want string
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
d := &HttpRequest{
|
||||
source: tt.fields.source,
|
||||
}
|
||||
if got := d.PostForm(tt.args.key); got != tt.want {
|
||||
t.Errorf("PostForm() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpRequest_Query(t *testing.T) {
|
||||
type fields struct {
|
||||
source *http.Request
|
||||
}
|
||||
type args struct {
|
||||
key string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
want string
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
d := &HttpRequest{
|
||||
source: tt.fields.source,
|
||||
}
|
||||
if got := d.Query(tt.args.key); got != tt.want {
|
||||
t.Errorf("Query() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpRequest_Source(t *testing.T) {
|
||||
type fields struct {
|
||||
source *http.Request
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
want interface{}
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
d := &HttpRequest{
|
||||
source: tt.fields.source,
|
||||
}
|
||||
if got := d.Source(); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("Source() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpRequest_Url(t *testing.T) {
|
||||
type fields struct {
|
||||
source *http.Request
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
want string
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
d := &HttpRequest{
|
||||
source: tt.fields.source,
|
||||
}
|
||||
if got := d.Url(); got != tt.want {
|
||||
t.Errorf("Url() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpResponse_AddHeader(t *testing.T) {
|
||||
type fields struct {
|
||||
DefaultRespImplement *ctx.DefaultRespImplement
|
||||
req *http.Request
|
||||
writer http.ResponseWriter
|
||||
}
|
||||
type args struct {
|
||||
name string
|
||||
value string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
r := &HttpResponse{
|
||||
DefaultRespImplement: tt.fields.DefaultRespImplement,
|
||||
req: tt.fields.req,
|
||||
writer: tt.fields.writer,
|
||||
}
|
||||
r.AddHeader(tt.args.name, tt.args.value)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpResponse_HTML(t *testing.T) {
|
||||
type fields struct {
|
||||
DefaultRespImplement *ctx.DefaultRespImplement
|
||||
req *http.Request
|
||||
writer http.ResponseWriter
|
||||
}
|
||||
type args struct {
|
||||
code int
|
||||
html string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
r := &HttpResponse{
|
||||
DefaultRespImplement: tt.fields.DefaultRespImplement,
|
||||
req: tt.fields.req,
|
||||
writer: tt.fields.writer,
|
||||
}
|
||||
if err := r.HTML(tt.args.code, tt.args.html); (err != nil) != tt.wantErr {
|
||||
t.Errorf("HTML() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpResponse_JSON(t *testing.T) {
|
||||
type fields struct {
|
||||
DefaultRespImplement *ctx.DefaultRespImplement
|
||||
req *http.Request
|
||||
writer http.ResponseWriter
|
||||
}
|
||||
type args struct {
|
||||
code int
|
||||
obj interface{}
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
r := &HttpResponse{
|
||||
DefaultRespImplement: tt.fields.DefaultRespImplement,
|
||||
req: tt.fields.req,
|
||||
writer: tt.fields.writer,
|
||||
}
|
||||
r.JSON(tt.args.code, tt.args.obj)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpResponse_Redirect(t *testing.T) {
|
||||
type fields struct {
|
||||
DefaultRespImplement *ctx.DefaultRespImplement
|
||||
req *http.Request
|
||||
writer http.ResponseWriter
|
||||
}
|
||||
type args struct {
|
||||
url string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
r := &HttpResponse{
|
||||
DefaultRespImplement: tt.fields.DefaultRespImplement,
|
||||
req: tt.fields.req,
|
||||
writer: tt.fields.writer,
|
||||
}
|
||||
r.Redirect(tt.args.url)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpResponse_SetHeader(t *testing.T) {
|
||||
type fields struct {
|
||||
DefaultRespImplement *ctx.DefaultRespImplement
|
||||
req *http.Request
|
||||
writer http.ResponseWriter
|
||||
}
|
||||
type args struct {
|
||||
name string
|
||||
value string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
r := &HttpResponse{
|
||||
DefaultRespImplement: tt.fields.DefaultRespImplement,
|
||||
req: tt.fields.req,
|
||||
writer: tt.fields.writer,
|
||||
}
|
||||
r.SetHeader(tt.args.name, tt.args.value)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpResponse_Source(t *testing.T) {
|
||||
type fields struct {
|
||||
DefaultRespImplement *ctx.DefaultRespImplement
|
||||
req *http.Request
|
||||
writer http.ResponseWriter
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
want interface{}
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
r := &HttpResponse{
|
||||
DefaultRespImplement: tt.fields.DefaultRespImplement,
|
||||
req: tt.fields.req,
|
||||
writer: tt.fields.writer,
|
||||
}
|
||||
if got := r.Source(); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("Source() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpResponse_Status(t *testing.T) {
|
||||
type fields struct {
|
||||
DefaultRespImplement *ctx.DefaultRespImplement
|
||||
req *http.Request
|
||||
writer http.ResponseWriter
|
||||
}
|
||||
type args struct {
|
||||
status int
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
r := &HttpResponse{
|
||||
DefaultRespImplement: tt.fields.DefaultRespImplement,
|
||||
req: tt.fields.req,
|
||||
writer: tt.fields.writer,
|
||||
}
|
||||
r.Status(tt.args.status)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewHttpRequest(t *testing.T) {
|
||||
type args struct {
|
||||
r *http.Request
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want *HttpRequest
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := NewHttpRequest(tt.args.r); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("NewHttpRequest() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewReqStorage(t *testing.T) {
|
||||
type args struct {
|
||||
req *http.Request
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want *HttpReqStorage
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := NewReqStorage(tt.args.req); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("NewReqStorage() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewResponse(t *testing.T) {
|
||||
type args struct {
|
||||
req *http.Request
|
||||
writer http.ResponseWriter
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want *HttpResponse
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := NewResponse(tt.args.req, tt.args.writer); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("NewResponse() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
30
ctx/go-http-context/request-storage.go
Normal file
30
ctx/go-http-context/request-storage.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package go_http_context
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type HttpReqStorage struct {
|
||||
source context.Context
|
||||
}
|
||||
|
||||
func NewReqStorage(req *http.Request) *HttpReqStorage {
|
||||
return &HttpReqStorage{source: req.Context()}
|
||||
}
|
||||
|
||||
func (r HttpReqStorage) Source() interface{} {
|
||||
return r.source
|
||||
}
|
||||
|
||||
func (r HttpReqStorage) Get(key string) interface{} {
|
||||
return r.source.Value(key)
|
||||
}
|
||||
|
||||
func (r HttpReqStorage) Set(key string, value string) {
|
||||
r.source = context.WithValue(r.source, key, value)
|
||||
}
|
||||
|
||||
func (r HttpReqStorage) Delete(key string) {
|
||||
r.source = context.WithValue(r.source, key, nil)
|
||||
}
|
52
ctx/go-http-context/request.go
Normal file
52
ctx/go-http-context/request.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package go_http_context
|
||||
|
||||
import (
|
||||
"github.com/weloe/token-go/ctx"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var _ ctx.Request = (*HttpRequest)(nil)
|
||||
|
||||
type HttpRequest struct {
|
||||
source *http.Request
|
||||
}
|
||||
|
||||
func NewHttpRequest(r *http.Request) *HttpRequest {
|
||||
return &HttpRequest{r}
|
||||
}
|
||||
|
||||
func (d *HttpRequest) Source() interface{} {
|
||||
return d.source
|
||||
}
|
||||
|
||||
func (d *HttpRequest) Header(key string) string {
|
||||
return d.source.Header.Get(key)
|
||||
}
|
||||
|
||||
func (d *HttpRequest) PostForm(key string) string {
|
||||
return d.source.PostFormValue(key)
|
||||
}
|
||||
|
||||
func (d *HttpRequest) Query(key string) string {
|
||||
return d.source.URL.Query().Get(key)
|
||||
}
|
||||
|
||||
func (d *HttpRequest) Path() string {
|
||||
return d.source.URL.Path
|
||||
}
|
||||
|
||||
func (d *HttpRequest) Url() string {
|
||||
return d.source.URL.String()
|
||||
}
|
||||
|
||||
func (d *HttpRequest) Method() string {
|
||||
return d.source.Method
|
||||
}
|
||||
|
||||
func (d *HttpRequest) Cookie(key string) string {
|
||||
cookie, err := d.source.Cookie(key)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return cookie.Value
|
||||
}
|
65
ctx/go-http-context/response.go
Normal file
65
ctx/go-http-context/response.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package go_http_context
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/weloe/token-go/ctx"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var _ ctx.Response = (*HttpResponse)(nil)
|
||||
|
||||
type HttpResponse struct {
|
||||
*ctx.DefaultRespImplement
|
||||
req *http.Request
|
||||
writer http.ResponseWriter
|
||||
}
|
||||
|
||||
func NewResponse(req *http.Request, writer http.ResponseWriter) *HttpResponse {
|
||||
return &HttpResponse{
|
||||
DefaultRespImplement: &ctx.DefaultRespImplement{},
|
||||
req: req,
|
||||
writer: writer,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *HttpResponse) Source() interface{} {
|
||||
return r.writer
|
||||
}
|
||||
|
||||
func (r *HttpResponse) SetHeader(name string, value string) {
|
||||
r.writer.Header().Set(name, value)
|
||||
}
|
||||
|
||||
func (r *HttpResponse) AddHeader(name string, value string) {
|
||||
r.writer.Header().Add(name, value)
|
||||
}
|
||||
|
||||
func (r *HttpResponse) Redirect(url string) {
|
||||
http.Redirect(r.writer, r.req, url, http.StatusTemporaryRedirect)
|
||||
}
|
||||
|
||||
func (r *HttpResponse) Status(status int) {
|
||||
r.writer.WriteHeader(status)
|
||||
}
|
||||
|
||||
// JSON response json data
|
||||
func (r *HttpResponse) JSON(code int, obj interface{}) {
|
||||
r.SetHeader("Content-Type", "application/json")
|
||||
r.Status(code)
|
||||
|
||||
encoder := json.NewEncoder(r.writer)
|
||||
if err := encoder.Encode(obj); err != nil {
|
||||
http.Error(r.writer, err.Error(), 500)
|
||||
}
|
||||
}
|
||||
|
||||
// HTML response .html
|
||||
func (r *HttpResponse) HTML(code int, html string) error {
|
||||
r.SetHeader("Content-Type", "text/html")
|
||||
r.Status(code)
|
||||
_, err := r.writer.Write([]byte(html))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
19
ctx/request.go
Normal file
19
ctx/request.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package ctx
|
||||
|
||||
type Request interface {
|
||||
Source() interface{}
|
||||
// Header get from request header
|
||||
Header(key string) string
|
||||
// PostForm get value from postForm
|
||||
PostForm(key string) string
|
||||
// Query https://example.org/?a=1&a=2&b=&=3&&&&" Query(a) return 1
|
||||
Query(key string) string
|
||||
// Path https://example.org/ex?a=1&a=2&b=&=3&&&& Path() return /ex
|
||||
Path() string
|
||||
// Url https://example.org//?a=1&a=2&b=&=3&&&& Url() return https://example.org/?a=1&a=2&b=&=3&&&&
|
||||
Url() string
|
||||
// Method request method
|
||||
Method() string
|
||||
// Cookie get value from cookie
|
||||
Cookie(key string) string
|
||||
}
|
10
ctx/request_storage.go
Normal file
10
ctx/request_storage.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package ctx
|
||||
|
||||
// ReqStorage
|
||||
// Use to set-get-delete data,and it'll be cleaned after request
|
||||
type ReqStorage interface {
|
||||
Source() interface{}
|
||||
Get(key string) interface{}
|
||||
Set(key string, value string)
|
||||
Delete(key string)
|
||||
}
|
12
ctx/response.go
Normal file
12
ctx/response.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package ctx
|
||||
|
||||
type Response interface {
|
||||
Source() interface{}
|
||||
DeleteCookie(name string, path string, domain string)
|
||||
AddCookie(name string, value string, path string, domain string, timeout int64)
|
||||
SetHeader(name string, value string)
|
||||
AddHeader(name string, value string)
|
||||
SetServer(value string)
|
||||
Redirect(url string)
|
||||
Status(status int)
|
||||
}
|
84
ctx/response_test.go
Normal file
84
ctx/response_test.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package ctx
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDefaultRespImplement_AddCookie(t *testing.T) {
|
||||
type args struct {
|
||||
name string
|
||||
value string
|
||||
path string
|
||||
domain string
|
||||
timeout int64
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
r := &DefaultRespImplement{}
|
||||
r.AddCookie(tt.args.name, tt.args.value, tt.args.path, tt.args.domain, tt.args.timeout)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultRespImplement_AddHeader(t *testing.T) {
|
||||
type args struct {
|
||||
name string
|
||||
value string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
r := &DefaultRespImplement{}
|
||||
r.AddHeader(tt.args.name, tt.args.value)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultRespImplement_DeleteCookie(t *testing.T) {
|
||||
type args struct {
|
||||
name string
|
||||
path string
|
||||
domain string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
r := &DefaultRespImplement{}
|
||||
r.DeleteCookie(tt.args.name, tt.args.path, tt.args.domain)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultRespImplement_SetServer(t *testing.T) {
|
||||
type args struct {
|
||||
value string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
r := &DefaultRespImplement{}
|
||||
r.SetServer(tt.args.value)
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user