github.com/xmplusdev/xmcore@v1.8.11-0.20240412132628-5518b55526af/common/protocol/http/sniff_test.go (about)

     1  package http_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "github.com/xmplusdev/xmcore/common/protocol/http"
     7  )
     8  
     9  func TestHTTPHeaders(t *testing.T) {
    10  	cases := []struct {
    11  		input  string
    12  		domain string
    13  		err    bool
    14  	}{
    15  		{
    16  			input: `GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
    17  Host: net.tutsplus.com
    18  User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)
    19  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    20  Accept-Language: en-us,en;q=0.5
    21  Accept-Encoding: gzip,deflate
    22  Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
    23  Keep-Alive: 300
    24  Connection: keep-alive
    25  Cookie: PHPSESSID=r2t5uvjq435r4q7ib3vtdjq120
    26  Pragma: no-cache
    27  Cache-Control: no-cache`,
    28  			domain: "net.tutsplus.com",
    29  		},
    30  		{
    31  			input: `POST /foo.php HTTP/1.1
    32  Host: localhost
    33  User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)
    34  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    35  Accept-Language: en-us,en;q=0.5
    36  Accept-Encoding: gzip,deflate
    37  Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
    38  Keep-Alive: 300
    39  Connection: keep-alive
    40  Referer: http://localhost/test.php
    41  Content-Type: application/x-www-form-urlencoded
    42  Content-Length: 43
    43   
    44  first_name=John&last_name=Doe&action=Submit`,
    45  			domain: "localhost",
    46  		},
    47  		{
    48  			input: `X /foo.php HTTP/1.1
    49  Host: localhost
    50  User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)
    51  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    52  Accept-Language: en-us,en;q=0.5
    53  Accept-Encoding: gzip,deflate
    54  Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
    55  Keep-Alive: 300
    56  Connection: keep-alive
    57  Referer: http://localhost/test.php
    58  Content-Type: application/x-www-form-urlencoded
    59  Content-Length: 43
    60   
    61  first_name=John&last_name=Doe&action=Submit`,
    62  			domain: "",
    63  			err:    true,
    64  		},
    65  		{
    66  			input: `GET /foo.php HTTP/1.1
    67  User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)
    68  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    69  Accept-Language: en-us,en;q=0.5
    70  Accept-Encoding: gzip,deflate
    71  Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
    72  Keep-Alive: 300
    73  Connection: keep-alive
    74  Referer: http://localhost/test.php
    75  Content-Type: application/x-www-form-urlencoded
    76  Content-Length: 43
    77  
    78  Host: localhost
    79  first_name=John&last_name=Doe&action=Submit`,
    80  			domain: "",
    81  			err:    true,
    82  		},
    83  		{
    84  			input:  `GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1`,
    85  			domain: "",
    86  			err:    true,
    87  		},
    88  	}
    89  
    90  	for _, test := range cases {
    91  		header, err := SniffHTTP([]byte(test.input))
    92  		if test.err {
    93  			if err == nil {
    94  				t.Errorf("Expect error but nil, in test: %v", test)
    95  			}
    96  		} else {
    97  			if err != nil {
    98  				t.Errorf("Expect no error but actually %s in test %v", err.Error(), test)
    99  			}
   100  			if header.Domain() != test.domain {
   101  				t.Error("expected domain ", test.domain, " but got ", header.Domain())
   102  			}
   103  		}
   104  	}
   105  }