github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/http/request_test.go (about) 1 /* For license and copyright information please see LEGAL file in repository */ 2 3 package http 4 5 import ( 6 "bytes" 7 "fmt" 8 "reflect" 9 "testing" 10 11 "../compress/raw" 12 ) 13 14 type RequestTest struct { 15 name string 16 packet []byte 17 req Request // expected parse 18 out Request // parsed one 19 } 20 21 var requestTests = []RequestTest{ 22 { 23 name: "simple1", 24 packet: []byte("GET / HTTP/1.1\r\n" + "\r\n"), 25 req: Request{ 26 method: "GET", 27 uri: URI{ 28 uri: "/", 29 uriAsByte: []byte("/"), 30 scheme: "", 31 authority: "", 32 path: "/", 33 query: "", 34 fragment: "", 35 }, 36 version: "HTTP/1.1", 37 H: header{}, 38 body: body{ 39 Codec: nil, 40 }, 41 }, 42 }, { 43 name: "full1", 44 packet: []byte("POST /m?2586547852 HTTP/1.1\r\n" + 45 "Accept: text/html\r\n" + 46 "Accept-Encoding: gzip, deflate\r\n" + 47 "Accept-Language: en,fa;q=0.9\r\n" + 48 "Cache-Control: max-age=0\r\n" + 49 "Connection: keep-alive\r\n" + 50 "Content-Length: 15\r\n" + 51 "Content-Type: application/json\r\n" + 52 "Host: www.sabz.city\r\n" + 53 "Set-Cookie: test\r\n" + 54 "Upgrade-Insecure-Requests: 1\r\n" + 55 "User-Agent: Mozilla\r\n" + 56 "\r\n" + 57 `{"Omid":"OMID"}`), 58 req: Request{ 59 method: "POST", 60 uri: URI{ 61 uri: "/m?2586547852", 62 uriAsByte: []byte("/m?2586547852"), 63 scheme: "", 64 authority: "", 65 path: "/m", 66 query: "2586547852", 67 fragment: "", 68 }, 69 version: "HTTP/1.1", 70 H: header{ 71 headers: map[string][]string{ 72 "Accept": []string{"text/html"}, 73 "Accept-Encoding": []string{"gzip, deflate"}, 74 "Accept-Language": []string{"en,fa;q=0.9"}, 75 "Cache-Control": []string{"max-age=0"}, 76 "Connection": []string{"keep-alive"}, 77 "Content-Length": []string{"15"}, 78 "Content-Type": []string{"application/json"}, 79 "Host": []string{"www.sabz.city"}, 80 "Set-Cookie": []string{"test"}, 81 "Upgrade-Insecure-Requests": []string{"1"}, 82 "User-Agent": []string{"Mozilla"}, 83 }, 84 }, 85 body: body{ 86 Codec: &raw.ComDecom{ 87 Data: []byte(`{"Omid":"OMID"}`), 88 }, 89 }, 90 }, 91 }, { 92 name: "without-body", 93 packet: []byte("POST /m?2586547852 HTTP/1.1\r\n" + 94 "Accept: text/html\r\n" + 95 "Accept-Encoding: gzip, deflate\r\n" + 96 "Accept-Language: en,fa;q=0.9\r\n" + 97 "Cache-Control: max-age=0\r\n" + 98 "Connection: keep-alive\r\n" + 99 "Content-Length: 15\r\n" + 100 "Content-Type: application/json\r\n" + 101 "Host: www.sabz.city\r\n" + 102 "Set-Cookie: test\r\n" + 103 "Upgrade-Insecure-Requests: 1\r\n" + 104 "User-Agent: Mozilla\r\n" + 105 "\r\n"), 106 req: Request{ 107 method: "POST", 108 uri: URI{ 109 uri: "/m?2586547852", 110 uriAsByte: []byte("/m?2586547852"), 111 scheme: "", 112 authority: "", 113 path: "/m", 114 query: "2586547852", 115 fragment: "", 116 }, 117 version: "HTTP/1.1", 118 H: header{ 119 headers: map[string][]string{ 120 "Accept": []string{"text/html"}, 121 "Accept-Encoding": []string{"gzip, deflate"}, 122 "Accept-Language": []string{"en,fa;q=0.9"}, 123 "Cache-Control": []string{"max-age=0"}, 124 "Connection": []string{"keep-alive"}, 125 "Content-Length": []string{"15"}, 126 "Content-Type": []string{"application/json"}, 127 "Host": []string{"www.sabz.city"}, 128 "Set-Cookie": []string{"test"}, 129 "Upgrade-Insecure-Requests": []string{"1"}, 130 "User-Agent": []string{"Mozilla"}, 131 }, 132 }, 133 body: body{ 134 Codec: nil, 135 }, 136 }, 137 }, 138 } 139 140 func TestRequest_Unmarshal(t *testing.T) { 141 for _, tt := range requestTests { 142 t.Run(tt.name, func(t *testing.T) { 143 tt.out.Init() 144 var err = tt.out.Unmarshal(tt.packet) 145 if err != nil { 146 t.Errorf("Request.Unmarshal() error = %v", err) 147 } 148 if tt.out.method != tt.req.method { 149 t.Errorf("Request.Unmarshal(%q) - Method:\n\tgot %v\n\twant %v\n", tt.packet, tt.out.method, tt.req.method) 150 } 151 if tt.out.uri.uri != tt.req.uri.uri { 152 t.Errorf("Request.Unmarshal(%q) - URI:\n\tgot %v\n\twant %v\n", tt.packet, tt.out.uri.uri, tt.req.uri.uri) 153 } 154 if tt.out.version != tt.req.version { 155 t.Errorf("Request.Unmarshal(%q) - Version:\n\tgot %v\n\twant %v\n", tt.packet, tt.out.version, tt.req.version) 156 } 157 // if !reflect.DeepEqual(tt.out.header.headers, tt.req.header.headers) { 158 // t.Errorf("Request.Unmarshal(%q):\n\tgot %v\n\twant %v\n", tt.packet, tt.out.header.headers, tt.req.header.headers) 159 // } 160 if !reflect.DeepEqual(tt.out.body, tt.req.body) { 161 t.Errorf("Request.Unmarshal(%q):\n\tgot %v\n\twant %v\n", tt.packet, tt.out.body, tt.req.body) 162 } 163 }) 164 } 165 166 // s := req.header.GetSetCookies()[0] 167 // err := s.CheckAndSanitize() 168 // fmt.Fprintf(os.Stderr, "%v\n", err) 169 // fmt.Fprintf(os.Stderr, "%v\n", s) 170 } 171 172 func TestRequest_Marshal(t *testing.T) { 173 for _, tt := range requestTests { 174 t.Run(tt.name, func(t *testing.T) { 175 var httpPacket []byte = tt.req.Marshal() 176 fmt.Println("cap--len of httpPacket:", cap(httpPacket), "--", len(httpPacket)) 177 fmt.Println("cap--len of tt.packet:", cap(tt.packet), "--", len(tt.packet)) 178 if httpPacket == nil { 179 t.Errorf("Request.Marshal() return nil!") 180 } 181 if !bytes.Equal(tt.packet, httpPacket) { 182 fmt.Println("encoded not same with original or just encode headers in not same order! ", tt.name) 183 fmt.Println(string(httpPacket)) 184 // t.Errorf("Request.Marshal(%q):\n\tgot %v\n\twant %v\n", tt.req, httpPacket, tt.packet) 185 } 186 }) 187 } 188 } 189 190 func BenchmarkRequest_Unmarshal(b *testing.B) { 191 for n := 0; n < b.N; n++ { 192 var httpReq Request 193 httpReq.Init() 194 httpReq.Unmarshal(requestTests[1].packet) 195 } 196 } 197 198 func BenchmarkRequest_Marshal(b *testing.B) { 199 for n := 0; n < b.N; n++ { 200 requestTests[1].req.Marshal() 201 } 202 }