github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/http/uri_test.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package http
     4  
     5  import (
     6  	"testing"
     7  )
     8  
     9  type uriTest struct {
    10  	name       string
    11  	raw        string
    12  	encoded    string
    13  	uri        URI // expected parse
    14  	out        URI // parsed one
    15  	wantURIEnd int
    16  }
    17  
    18  var uriTests = []uriTest{
    19  	{
    20  		name:    "asterisk-form",
    21  		raw:     "* ",
    22  		encoded: "*",
    23  		uri: URI{
    24  			uri:       "*",
    25  			scheme:    "",
    26  			authority: "",
    27  			path:      "",
    28  			query:     "",
    29  			fragment:  "",
    30  		},
    31  		wantURIEnd: 1,
    32  	}, {
    33  		name:    "simple path",
    34  		raw:     "/ ",
    35  		encoded: "/",
    36  		uri: URI{
    37  			uri:       "/",
    38  			scheme:    "",
    39  			authority: "",
    40  			path:      "/",
    41  			query:     "",
    42  			fragment:  "",
    43  		},
    44  		wantURIEnd: 1,
    45  	}, {
    46  		name:    "origin-form1",
    47  		raw:     "/m?2586547852#api ",
    48  		encoded: "/m?2586547852",
    49  		uri: URI{
    50  			uri:       "/m?2586547852#api",
    51  			scheme:    "",
    52  			authority: "",
    53  			path:      "/m",
    54  			query:     "2586547852",
    55  			fragment:  "api",
    56  		},
    57  		wantURIEnd: 17,
    58  	}, {
    59  		name:    "origin-form2",
    60  		raw:     "/action/do/show/411?2586547852#Test ",
    61  		encoded: "/action/do/show/411?2586547852",
    62  		uri: URI{
    63  			uri:       "/action/do/show/411?2586547852#Test",
    64  			scheme:    "",
    65  			authority: "",
    66  			path:      "/action/do/show/411",
    67  			query:     "2586547852",
    68  			fragment:  "Test",
    69  		},
    70  		wantURIEnd: 35,
    71  	}, {
    72  		name:    "absolute-URI1",
    73  		raw:     "https://tools.ietf.org/html/rfc2616#section-3.2 ",
    74  		encoded: "https://tools.ietf.org/html/rfc2616",
    75  		uri: URI{
    76  			uri:       "https://tools.ietf.org/html/rfc2616#section-3.2",
    77  			scheme:    "https",
    78  			authority: "tools.ietf.org",
    79  			path:      "/html/rfc2616",
    80  			query:     "",
    81  			fragment:  "section-3.2",
    82  		},
    83  		wantURIEnd: 47,
    84  	}, {
    85  		name:    "absolute-URI2",
    86  		raw:     "http://www.sabz.city/#file%20one%26two ",
    87  		encoded: "http://www.sabz.city/",
    88  		uri: URI{
    89  			uri:       "http://www.sabz.city/#file%20one%26two",
    90  			scheme:    "http",
    91  			authority: "www.sabz.city",
    92  			path:      "/",
    93  			query:     "",
    94  			fragment:  "file%20one%26two",
    95  		},
    96  		wantURIEnd: 38,
    97  	}, {
    98  		name:    "absolute-URI3",
    99  		raw:     "https://www.sabz.city/pub/WWW/TheProject.html ",
   100  		encoded: "https://www.sabz.city/pub/WWW/TheProject.html",
   101  		uri: URI{
   102  			uri:       "https://www.sabz.city/pub/WWW/TheProject.html",
   103  			scheme:    "https",
   104  			authority: "www.sabz.city",
   105  			path:      "/pub/WWW/TheProject.html",
   106  			query:     "",
   107  			fragment:  "",
   108  		},
   109  		wantURIEnd: 45,
   110  	}, {
   111  		name:    "absolute-URI4",
   112  		raw:     "www.sabz.city/m?2586547852#api ",
   113  		encoded: "www.sabz.city/m?2586547852",
   114  		uri: URI{
   115  			uri:       "www.sabz.city/m?2586547852#api",
   116  			scheme:    "",
   117  			authority: "www.sabz.city",
   118  			path:      "/m",
   119  			query:     "2586547852",
   120  			fragment:  "api",
   121  		},
   122  		wantURIEnd: 30,
   123  	}, {
   124  		name:    "ftp1",
   125  		raw:     "ftp://webmaster@www.sabz.city/ ",
   126  		encoded: "ftp://webmaster@www.sabz.city/",
   127  		uri: URI{
   128  			uri:       "ftp://webmaster@www.sabz.city/",
   129  			scheme:    "ftp",
   130  			authority: "webmaster@www.sabz.city",
   131  			path:      "/",
   132  			query:     "",
   133  			fragment:  "",
   134  		},
   135  		wantURIEnd: 30,
   136  	}, {
   137  		name:    "empty query",
   138  		raw:     "http://www.sabz.city/? ",
   139  		encoded: "http://www.sabz.city/",
   140  		uri: URI{
   141  			uri:       "http://www.sabz.city/?",
   142  			scheme:    "http",
   143  			authority: "www.sabz.city",
   144  			path:      "/",
   145  			query:     "",
   146  			fragment:  "",
   147  		},
   148  		wantURIEnd: 22,
   149  	},
   150  }
   151  
   152  func TestURI_Unmarshal(t *testing.T) {
   153  	for _, tt := range uriTests {
   154  		t.Run(tt.name, func(t *testing.T) {
   155  			var gotURIEnd = tt.out.unmarshalFrom(tt.raw)
   156  			if gotURIEnd != tt.wantURIEnd {
   157  				t.Errorf("URI.Unmarshal(%q) = %v, want %v",tt.name, gotURIEnd, tt.wantURIEnd)
   158  			}
   159  			if tt.out.scheme != tt.uri.scheme || tt.out.authority != tt.uri.authority || tt.out.path != tt.uri.path || tt.out.query != tt.uri.query || tt.out.fragment != tt.uri.fragment {
   160  				t.Errorf("URI.Unmarshal(%q):\n\tgot  %v\n\twant %v\n", tt.name, tt.out, tt.uri)
   161  			}
   162  		})
   163  	}
   164  }
   165  
   166  func TestURI_Marshal(t *testing.T) {
   167  	for i := 1; i < len(uriTests); i++ { // start from 1 due to asterisk-form is not general form that we can use Set() method
   168  		var uriTest = uriTests[i]
   169  		uriTest.uri.Set(uriTest.uri.scheme, uriTest.uri.authority, uriTest.uri.path, uriTest.uri.query)
   170  		t.Run(uriTest.name, func(t *testing.T) {
   171  			var httpPacket = uriTest.uri.Marshal()
   172  			if uriTest.encoded != string(httpPacket) {
   173  				t.Errorf("URI.Unmarshal():\n\tgot  %v\n\twant %v\n", string(httpPacket), uriTest.encoded)
   174  			}
   175  		})
   176  	}
   177  }