github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/http/request_test.go (about)

     1  /* For license and copyright information please see the LEGAL file in the code repository */
     2  
     3  package http
     4  
     5  import (
     6  	"bytes"
     7  	"fmt"
     8  	"reflect"
     9  	"testing"
    10  
    11  	"github.com/GeniusesGroup/libgo/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.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.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":                    {"text/html"},
    73  					"Accept-Encoding":           {"gzip, deflate"},
    74  					"Accept-Language":           {"en,fa;q=0.9"},
    75  					"Cache-Control":             {"max-age=0"},
    76  					"Connection":                {"keep-alive"},
    77  					"Content-Length":            {"15"},
    78  					"Content-Type":              {"application/json"},
    79  					"Host":                      {"www.sabz.city"},
    80  					"Set-Cookie":                {"test"},
    81  					"Upgrade-Insecure-Requests": {"1"},
    82  					"User-Agent":                {"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.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":                    {"text/html"},
   121  					"Accept-Encoding":           {"gzip, deflate"},
   122  					"Accept-Language":           {"en,fa;q=0.9"},
   123  					"Cache-Control":             {"max-age=0"},
   124  					"Connection":                {"keep-alive"},
   125  					"Content-Length":            {"15"},
   126  					"Content-Type":              {"application/json"},
   127  					"Host":                      {"www.sabz.city"},
   128  					"Set-Cookie":                {"test"},
   129  					"Upgrade-Insecure-Requests": {"1"},
   130  					"User-Agent":                {"Mozilla"},
   131  				},
   132  			},
   133  			body: body{
   134  				Codec: nil,
   135  			},
   136  		},
   137  	},
   138  }
   139  
   140  func init() {
   141  	requestTests[0].req.uri.Set("", "", "/", "", "")
   142  	requestTests[1].req.uri.Set("", "", "/m", "2586547852", "")
   143  	requestTests[2].req.uri.Set("", "", "/m", "2586547852", "")
   144  }
   145  
   146  func TestRequest_Unmarshal(t *testing.T) {
   147  	for _, tt := range requestTests {
   148  		t.Run(tt.name, func(t *testing.T) {
   149  			tt.out.Init()
   150  			var _, err = tt.out.Unmarshal(tt.packet)
   151  			if err != nil {
   152  				t.Errorf("Request.Unmarshal() error = %v", err)
   153  			}
   154  			if tt.out.method != tt.req.method {
   155  				t.Errorf("Request.Unmarshal(%q) - Method:\n\tgot  %v\n\twant %v\n", tt.packet, tt.out.method, tt.req.method)
   156  			}
   157  			if tt.out.uri.URI() != tt.req.uri.URI() {
   158  				t.Errorf("Request.Unmarshal(%q) - URI:\n\tgot  %v\n\twant %v\n", tt.packet, tt.out.uri.URI(), tt.req.uri.URI())
   159  			}
   160  			if tt.out.version != tt.req.version {
   161  				t.Errorf("Request.Unmarshal(%q) - Version:\n\tgot  %v\n\twant %v\n", tt.packet, tt.out.version, tt.req.version)
   162  			}
   163  			// if !reflect.DeepEqual(tt.out.header.headers, tt.req.header.headers) {
   164  			// 	t.Errorf("Request.Unmarshal(%q):\n\tgot  %v\n\twant %v\n", tt.packet, tt.out.header.headers, tt.req.header.headers)
   165  			// }
   166  			if !reflect.DeepEqual(tt.out.body, tt.req.body) {
   167  				t.Errorf("Request.Unmarshal(%q):\n\tgot  %v\n\twant %v\n", tt.packet, tt.out.body, tt.req.body)
   168  			}
   169  		})
   170  	}
   171  
   172  	// s := req.header.GetSetCookies()[0]
   173  	// err := s.CheckAndSanitize()
   174  	// fmt.Fprintf(os.Stderr, "%v\n", err)
   175  	// fmt.Fprintf(os.Stderr, "%v\n", s)
   176  }
   177  
   178  func TestRequest_Marshal(t *testing.T) {
   179  	for _, tt := range requestTests {
   180  		t.Run(tt.name, func(t *testing.T) {
   181  			var httpPacket, _ = tt.req.Marshal()
   182  			fmt.Println("cap--len of httpPacket:", cap(httpPacket), "--", len(httpPacket))
   183  			fmt.Println("cap--len of tt.packet:", cap(tt.packet), "--", len(tt.packet))
   184  			if httpPacket == nil {
   185  				t.Errorf("Request.Marshal() return nil!")
   186  			}
   187  			if !bytes.Equal(tt.packet, httpPacket) {
   188  				fmt.Println("encoded not same with original or just encode headers in not same order! ", tt.name)
   189  				fmt.Println(string(httpPacket))
   190  				// t.Errorf("Request.Marshal(%q):\n\tgot  %v\n\twant %v\n", tt.req, httpPacket, tt.packet)
   191  			}
   192  		})
   193  	}
   194  }
   195  
   196  func BenchmarkRequest_Unmarshal(b *testing.B) {
   197  	for n := 0; n < b.N; n++ {
   198  		var httpReq Request
   199  		httpReq.Init()
   200  		httpReq.Unmarshal(requestTests[1].packet)
   201  	}
   202  }
   203  
   204  func BenchmarkRequest_Marshal(b *testing.B) {
   205  	for n := 0; n < b.N; n++ {
   206  		requestTests[1].req.Marshal()
   207  	}
   208  }