package go_http_context import ( "encoding/json" "fmt" "github.com/weloe/token-go/ctx" "net/http" "net/http/httptest" "net/url" "strings" "testing" "time" ) func NewTestRequest(t *testing.T) *http.Request { req, err := http.NewRequest("GET", "https://baidu.com/api/", strings.NewReader("")) // cookie cookie := &http.Cookie{Name: "myCookie", Value: "cookieValue"} req.AddCookie(cookie) // header req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("User-Agent", "My Custom User Agent") // POST form form := url.Values{} form.Add("key1", "value1") form.Add("key2", "value2") req.PostForm = form // add query q := req.URL.Query() q.Add("query1", "value1") q.Add("query2", "value2") req.URL.RawQuery = q.Encode() if err != nil { t.Errorf("new request error: %v", err) } return req } func NewTestHttpRequest(t *testing.T) *HttpRequest { request := NewTestRequest(t) httpRequest := NewHttpRequest(request) return httpRequest } func NewTestHttpReqStore(t *testing.T) *HttpReqStorage { request := NewTestRequest(t) httpReqStorage := NewReqStorage(request) return httpReqStorage } func TestHttpContext_IsValidContext(t *testing.T) { type fields struct { req ctx.Request response ctx.Response reqStorage ctx.ReqStorage } request := NewTestRequest(t) 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) { httpReqStorage := NewTestHttpReqStore(t) httpReqStorage.Set("s", "k") httpReqStorage.Delete("s") get := httpReqStorage.Get("s") if get != nil { t.Errorf("Delete error") } } func TestHttpReqStorage_Get(t *testing.T) { httpReqStorage := NewTestHttpReqStore(t) httpReqStorage.Set("s", "k") k := fmt.Sprintf("%v", httpReqStorage.Get("s")) if k != "k" { t.Errorf("get method error,Get() = %s want 'k'", k) } } func TestHttpRequest_Cookie(t *testing.T) { request := NewTestHttpRequest(t) cookie := request.Cookie("err") if cookie != "" { t.Errorf("Cookie() = %v,want ' '", cookie) } cookie = request.Cookie("myCookie") if cookie != "cookieValue" { t.Errorf("Cookie() = %v,want cookieValue", cookie) } } func TestHttpRequest_Header(t *testing.T) { request := NewTestHttpRequest(t) hV := request.Header("err") if hV != "" { t.Errorf("Header() = %v,want ' '", hV) } hV = request.Header("Content-Type") if hV != "application/x-www-form-urlencoded" { t.Errorf("Header() = %v,want application/x-www-form-urlencoded", hV) } } func TestHttpRequest_Method(t *testing.T) { request := NewTestHttpRequest(t) if request.Method() != "GET" { t.Errorf("Method() = %s,want GET", request.Method()) } } func TestHttpRequest_Path(t *testing.T) { request := NewTestHttpRequest(t) t.Log(request.Path()) if request.Path() != "/api/" { t.Errorf("Path() = %s want /api/", request.Path()) } } func TestHttpRequest_PostForm(t *testing.T) { request := NewTestHttpRequest(t) if request.PostForm("key") != "" { t.Errorf("PostForm() = %s want ' '", request.PostForm("key")) } if request.PostForm("key1") != "value1" { t.Errorf("PostForm() = %s want ' '", request.PostForm("key1")) } if request.PostForm("key2") != "value2" { t.Errorf("PostForm() = %s want ' '", request.PostForm("key2")) } } func TestHttpRequest_Query(t *testing.T) { request := NewTestHttpRequest(t) if request.Query("key") != "" { t.Errorf("Query() = %s want ' '", request.Query("key")) } if request.Query("query1") != "value1" { t.Errorf("Query() = %s want ' '", request.Query("query1")) } if request.Query("query2") != "value2" { t.Errorf("Query() = %s want ' '", request.Query("query2")) } } func TestHttpRequest_Source(t *testing.T) { request := NewTestHttpRequest(t) t.Log(request.Source()) } func TestHttpRequest_Url(t *testing.T) { s := NewTestHttpRequest(t).Url() if s != "https://baidu.com/api/?query1=value1&query2=value2" { t.Errorf("Url() = %s ,want https://baidu.com/api/?query1=value1&query2=value2", s) } } func TestHttpResponse_SetHeader(t *testing.T) { req, _ := http.NewRequest(http.MethodGet, "/", nil) w := httptest.NewRecorder() resp := NewResponse(req, w) resp.SetHeader("Content-Type", "application/json") if ct := w.Header().Get("Content-Type"); ct != "application/json" { t.Errorf("SetHeader() failed: expected Content-Type header to be 'application/json', got '%s'", ct) } } func TestHttpResponse_AddHeader(t *testing.T) { req, _ := http.NewRequest(http.MethodGet, "/", nil) w := httptest.NewRecorder() resp := NewResponse(req, w) resp.AddHeader("Content-Type", "application/json") resp.AddHeader("Cache-Control", "no-cache") if ct := w.Header().Get("Content-Type"); ct != "application/json" { t.Errorf("AddHeader() failed: expected Content-Type header to be 'application/json', got '%s'", ct) } if cc := w.Header().Get("Cache-Control"); cc != "no-cache" { t.Errorf("AddHeader() failed: expected Cache-Control header to be 'no-cache', got '%s'", cc) } } func TestHttpResponse_Redirect(t *testing.T) { req, _ := http.NewRequest(http.MethodGet, "/", nil) w := httptest.NewRecorder() resp := NewResponse(req, w) resp.Redirect("/new-page") if w.Code != http.StatusTemporaryRedirect { t.Errorf("Redirect() failed: expected status code %d, got %d", http.StatusTemporaryRedirect, w.Code) } if loc := w.Header().Get("Location"); loc != "/new-page" { t.Errorf("Redirect() failed: expected Location header to be '/new-page', got '%s'", loc) } } func TestHttpResponse_Status(t *testing.T) { req, _ := http.NewRequest(http.MethodGet, "/", nil) w := httptest.NewRecorder() resp := NewResponse(req, w) resp.Status(http.StatusOK) if w.Code != http.StatusOK { t.Errorf("Status() failed: expected status code %d, got %d", http.StatusOK, w.Code) } } func TestHttpResponse_JSON(t *testing.T) { req, _ := http.NewRequest(http.MethodGet, "/", nil) w := httptest.NewRecorder() resp := NewResponse(req, w) type Person struct { Name string `json:"name"` Age int `json:"age"` } person := &Person{Name: "John Doe", Age: 30} resp.JSON(http.StatusOK, person) if w.Code != http.StatusOK { t.Errorf("JSON() failed: expected status code %d, got %d", http.StatusOK, w.Code) } if ct := w.Header().Get("Content-Type"); ct != "application/json" { t.Errorf("JSON() failed: expected Content-Type header to be 'application/json', got '%s'", ct) } var p Person decoder := json.NewDecoder(w.Body) err := decoder.Decode(&p) if err != nil { t.Fatalf("Error decoding response body: %s", err) } if p.Name != "John Doe" { t.Errorf("JSON() failed: expected person name to be 'John Doe', got '%s'", p.Name) } if p.Age != 30 { t.Errorf("JSON() failed: expected person age to be '30', got '%d'", p.Age) } } func TestHttpResponse_HTML(t *testing.T) { req, _ := http.NewRequest(http.MethodGet, "/", nil) w := httptest.NewRecorder() resp := NewResponse(req, w) htmlCode := "