github.com/cloudwego/kitex@v0.9.0/pkg/generic/descriptor/http_test.go (about) 1 /* 2 * Copyright 2023 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package descriptor 18 19 import ( 20 "bytes" 21 "net/http" 22 "net/url" 23 "reflect" 24 "testing" 25 26 "github.com/cloudwego/kitex/internal/test" 27 ) 28 29 func TestJSONBody(t *testing.T) { 30 data := []byte(`{"name":"foo","age":18}`) 31 r, err := createHTTPRequest(data) 32 if err != nil { 33 t.Fatal(err) 34 } 35 test.Assert(t, r.GetHeader("Content-Type") == "application/json") 36 test.Assert(t, r.GetCookie("token") == "some_token") 37 test.Assert(t, r.GetQuery("id") == "123") 38 test.Assert(t, r.GetParam("param1") == "value1") 39 test.Assert(t, reflect.DeepEqual(r.GetBody(), data)) 40 test.Assert(t, r.GetMethod() == "POST") 41 test.Assert(t, r.GetPath() == "/users") 42 test.Assert(t, r.GetHost() == "localhost:8080") 43 test.Assert(t, r.GetMapBody("name") == `foo`) 44 test.Assert(t, r.GetMapBody("age") == "18") 45 test.Assert(t, r.GetPostForm("key1") == "val1") 46 test.Assert(t, r.GetUri() == "http://localhost:8080/users?id=123", r.GetUri()) 47 48 resp := NewHTTPResponse() 49 resp.SetStatusCode(200) 50 resp.SetHeader("Content-Type", "application/json") 51 resp.SetCookie("token", "some-token") 52 resp.SetRawBody(data) 53 test.Assert(t, resp.StatusCode == 200) 54 test.Assert(t, resp.Header.Get("Content-Type") == "application/json") 55 test.Assert(t, resp.ContentType == "application/json") 56 test.Assert(t, reflect.DeepEqual(resp.RawBody, data)) 57 } 58 59 func TestMapBody(t *testing.T) { 60 r, err := createHTTPRequest([]byte(``)) 61 if err != nil { 62 t.Fatal(err) 63 } 64 err = r.initializeBodyMap() 65 test.Assert(t, err != nil) 66 test.Assert(t, r.GetMapBody("test") == "") 67 r, err = createHTTPRequest([]byte(`{"name":"foo","age":18}`)) 68 if err != nil { 69 t.Fatal(err) 70 } 71 test.Assert(t, r.bodyMap == nil) 72 err = r.initializeBodyMap() 73 test.Assert(t, err == nil) 74 test.Assert(t, r.bodyMap != nil) 75 test.Assert(t, r.GetMapBody("name") == `foo`) 76 test.Assert(t, r.GetMapBody("test") == "") 77 } 78 79 func TestGetQuery(t *testing.T) { 80 r, err := createHTTPRequest([]byte(`{"name":"foo","age":18}`)) 81 if err != nil { 82 t.Fatal(err) 83 } 84 test.Assert(t, r.query == nil) 85 test.Assert(t, r.GetQuery("id") == "123") 86 test.Assert(t, r.query != nil) 87 } 88 89 func TestGetCookie(t *testing.T) { 90 r, err := createHTTPRequest([]byte(`{"name":"foo","age":18}`)) 91 if err != nil { 92 t.Fatal(err) 93 } 94 test.Assert(t, r.cookies == nil) 95 test.Assert(t, r.GetCookie("token") == "some_token") 96 test.Assert(t, r.cookies != nil) 97 } 98 99 func TestNilURL(t *testing.T) { 100 r, err := createHTTPRequestWithNoURL([]byte("")) 101 if err != nil { 102 t.Fatal(err) 103 } 104 test.Assert(t, r.Request.URL == nil) 105 test.Assert(t, r.GetQuery("") == "") 106 test.Assert(t, r.GetPath() == "") 107 test.Assert(t, r.GetUri() == "") 108 } 109 110 func createHTTPRequestWithNoURL(data []byte) (*HTTPRequest, error) { 111 req, err := http.NewRequest("POST", "", bytes.NewBuffer(data)) 112 if err != nil { 113 return nil, err 114 } 115 req.URL = nil 116 param := Param{ 117 Key: "param1", 118 Value: "value1", 119 } 120 pslice := []Param{param} 121 params := Params{ 122 params: pslice, 123 } 124 r := &HTTPRequest{ 125 Request: req, 126 RawBody: data, 127 Params: ¶ms, 128 } 129 return r, nil 130 } 131 132 func createHTTPRequest(data []byte) (*HTTPRequest, error) { 133 req, err := http.NewRequest("POST", "http://localhost:8080/users?id=123", bytes.NewBuffer(data)) 134 if err != nil { 135 return nil, err 136 } 137 req.Header.Set("Content-Type", "application/json") 138 cookie := &http.Cookie{ 139 Name: "token", 140 Value: "some_token", 141 } 142 req.AddCookie(cookie) 143 req.URL.Path = "/users" 144 req.PostForm = url.Values{ 145 "key1": {"val1", "val2"}, 146 } 147 param := Param{ 148 Key: "param1", 149 Value: "value1", 150 } 151 pslice := []Param{param} 152 params := Params{ 153 params: pslice, 154 } 155 r := &HTTPRequest{ 156 Request: req, 157 RawBody: data, 158 Params: ¶ms, 159 } 160 return r, nil 161 }