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

     1  /* For license and copyright information please see the LEGAL file in the code repository */
     2  
     3  package uri
     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  			AU: Authority{
    27  				authority: "",
    28  			},
    29  			path:     "",
    30  			query:    "",
    31  			fragment: "",
    32  		},
    33  		wantURIEnd: 1,
    34  	}, {
    35  		name:    "simple path",
    36  		raw:     "/ ",
    37  		encoded: "/",
    38  		uri: URI{
    39  			uri:    "/",
    40  			scheme: "",
    41  			AU: Authority{
    42  				authority: "",
    43  			},
    44  			path:     "/",
    45  			query:    "",
    46  			fragment: "",
    47  		},
    48  		wantURIEnd: 1,
    49  	}, {
    50  		name:    "origin-form1",
    51  		raw:     "/m?2586547852#api ",
    52  		encoded: "/m?2586547852",
    53  		uri: URI{
    54  			uri:    "/m?2586547852#api",
    55  			scheme: "",
    56  			AU: Authority{
    57  				authority: "",
    58  			},
    59  			path:     "/m",
    60  			query:    "2586547852",
    61  			fragment: "api",
    62  		},
    63  		wantURIEnd: 17,
    64  	}, {
    65  		name:    "origin-form2",
    66  		raw:     "/action/do/show/411?2586547852#Test ",
    67  		encoded: "/action/do/show/411?2586547852",
    68  		uri: URI{
    69  			uri:    "/action/do/show/411?2586547852#Test",
    70  			scheme: "",
    71  			AU: Authority{
    72  				authority: "",
    73  			},
    74  			path:     "/action/do/show/411",
    75  			query:    "2586547852",
    76  			fragment: "Test",
    77  		},
    78  		wantURIEnd: 35,
    79  	}, {
    80  		name:    "absolute-URI1",
    81  		raw:     "https://tools.ietf.org/html/rfc2616#section-3.2 ",
    82  		encoded: "https://tools.ietf.org/html/rfc2616",
    83  		uri: URI{
    84  			uri:    "https://tools.ietf.org/html/rfc2616#section-3.2",
    85  			scheme: "https",
    86  			AU: Authority{
    87  				authority: "tools.ietf.org",
    88  			},
    89  			path:     "/html/rfc2616",
    90  			query:    "",
    91  			fragment: "section-3.2",
    92  		},
    93  		wantURIEnd: 47,
    94  	}, {
    95  		name:    "absolute-URI2",
    96  		raw:     "http://www.sabz.city/#file%20one%26two ",
    97  		encoded: "http://www.sabz.city/",
    98  		uri: URI{
    99  			uri:    "http://www.sabz.city/#file%20one%26two",
   100  			scheme: "http",
   101  			AU: Authority{
   102  				authority: "www.sabz.city",
   103  			},
   104  			path:     "/",
   105  			query:    "",
   106  			fragment: "file%20one%26two",
   107  		},
   108  		wantURIEnd: 38,
   109  	}, {
   110  		name:    "absolute-URI3",
   111  		raw:     "https://www.sabz.city/pub/WWW/TheProject.html ",
   112  		encoded: "https://www.sabz.city/pub/WWW/TheProject.html",
   113  		uri: URI{
   114  			uri:    "https://www.sabz.city/pub/WWW/TheProject.html",
   115  			scheme: "https",
   116  			AU: Authority{
   117  				authority: "www.sabz.city",
   118  			},
   119  			path:     "/pub/WWW/TheProject.html",
   120  			query:    "",
   121  			fragment: "",
   122  		},
   123  		wantURIEnd: 45,
   124  	}, {
   125  		name:    "absolute-URI4",
   126  		raw:     "www.sabz.city/m?2586547852#api ",
   127  		encoded: "www.sabz.city/m?2586547852",
   128  		uri: URI{
   129  			uri:    "www.sabz.city/m?2586547852#api",
   130  			scheme: "",
   131  			AU: Authority{
   132  				authority: "www.sabz.city",
   133  			},
   134  			path:     "/m",
   135  			query:    "2586547852",
   136  			fragment: "api",
   137  		},
   138  		wantURIEnd: 30,
   139  	}, {
   140  		name:    "ftp1",
   141  		raw:     "ftp://webmaster@www.sabz.city/ ",
   142  		encoded: "ftp://webmaster@www.sabz.city/",
   143  		uri: URI{
   144  			uri:    "ftp://webmaster@www.sabz.city/",
   145  			scheme: "ftp",
   146  			AU: Authority{
   147  				authority: "webmaster@www.sabz.city",
   148  			},
   149  			path:     "/",
   150  			query:    "",
   151  			fragment: "",
   152  		},
   153  		wantURIEnd: 30,
   154  	}, {
   155  		name:    "empty query",
   156  		raw:     "http://www.sabz.city/? ",
   157  		encoded: "http://www.sabz.city/",
   158  		uri: URI{
   159  			uri:    "http://www.sabz.city/?",
   160  			scheme: "http",
   161  			AU: Authority{
   162  				authority: "www.sabz.city",
   163  			},
   164  			path:     "/",
   165  			query:    "",
   166  			fragment: "",
   167  		},
   168  		wantURIEnd: 22,
   169  	}, {
   170  		name:    "embed uri in query",
   171  		raw:     "http://www.sabz.city/repo?n=libgo&m=modules/app/ ",
   172  		encoded: "http://www.sabz.city/repo?n=libgo&m=modules/app/",
   173  		uri: URI{
   174  			uri:    "http://www.sabz.city/repo?n=libgo&m=modules/app/",
   175  			scheme: "http",
   176  			AU: Authority{
   177  				authority: "www.sabz.city",
   178  			},
   179  			path:     "/repo",
   180  			query:    "n=libgo&m=modules/app/",
   181  			fragment: "",
   182  		},
   183  		wantURIEnd: 48,
   184  	},
   185  }
   186  
   187  func TestURI_Unmarshal(t *testing.T) {
   188  	for _, tt := range uriTests {
   189  		t.Run(tt.name, func(t *testing.T) {
   190  			var gotURIEnd = tt.out.UnmarshalFromString(tt.raw)
   191  			if gotURIEnd != tt.wantURIEnd {
   192  				t.Errorf("URI.Unmarshal(%q) = %v, want %v", tt.name, gotURIEnd, tt.wantURIEnd)
   193  			}
   194  			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 {
   195  				t.Errorf("URI.Unmarshal(%q):\n\tgot  %v\n\twant %v\n", tt.name, tt.out, tt.uri)
   196  			}
   197  		})
   198  	}
   199  }
   200  
   201  func TestURI_Marshal(t *testing.T) {
   202  	for i := 1; i < len(uriTests); i++ { // start from 1 due to asterisk-form is not general form that we can use Set() method
   203  		var uriTest = uriTests[i]
   204  		uriTest.uri.Set(uriTest.uri.scheme, uriTest.uri.authority, uriTest.uri.path, uriTest.uri.query, uriTest.uri.fragment)
   205  		t.Run(uriTest.name, func(t *testing.T) {
   206  			var httpPacket = uriTest.uri.Marshal()
   207  			if uriTest.encoded != string(httpPacket) {
   208  				t.Errorf("URI.Unmarshal():\n\tgot  %v\n\twant %v\n", string(httpPacket), uriTest.encoded)
   209  			}
   210  		})
   211  	}
   212  }