| 
									
										
										
										
											2016-01-30 01:31:00 +08:00
										 |  |  | package util | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2019-01-29 05:18:48 +08:00
										 |  |  | 	"net/url" | 
					
						
							| 
									
										
										
										
											2016-01-30 01:31:00 +08:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-13 17:10:48 +08:00
										 |  |  | 	"github.com/stretchr/testify/assert" | 
					
						
							|  |  |  | 	"github.com/stretchr/testify/require" | 
					
						
							| 
									
										
										
										
											2016-01-30 01:31:00 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-13 17:10:48 +08:00
										 |  |  | func TestJoinURLFragments(t *testing.T) { | 
					
						
							|  |  |  | 	t.Parallel() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		description string | 
					
						
							|  |  |  | 		base        string | 
					
						
							|  |  |  | 		path        string | 
					
						
							|  |  |  | 		expected    string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			description: "RHS is empty", | 
					
						
							|  |  |  | 			base:        "http://localhost:8080", | 
					
						
							|  |  |  | 			path:        "", | 
					
						
							|  |  |  | 			expected:    "http://localhost:8080", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			description: "RHS is empty and LHS has trailing slash", | 
					
						
							|  |  |  | 			base:        "http://localhost:8080/", | 
					
						
							|  |  |  | 			path:        "", | 
					
						
							|  |  |  | 			expected:    "http://localhost:8080/", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			description: "neither has trailing slash", | 
					
						
							|  |  |  | 			base:        "http://localhost:8080", | 
					
						
							|  |  |  | 			path:        "api", | 
					
						
							|  |  |  | 			expected:    "http://localhost:8080/api", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			description: "LHS has trailing slash", | 
					
						
							|  |  |  | 			base:        "http://localhost:8080/", | 
					
						
							|  |  |  | 			path:        "api", | 
					
						
							|  |  |  | 			expected:    "http://localhost:8080/api", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			description: "LHS and RHS has trailing slash", | 
					
						
							|  |  |  | 			base:        "http://localhost:8080/", | 
					
						
							|  |  |  | 			path:        "api/", | 
					
						
							|  |  |  | 			expected:    "http://localhost:8080/api/", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			description: "LHS has trailing slash and RHS has preceding slash", | 
					
						
							|  |  |  | 			base:        "http://localhost:8080/", | 
					
						
							|  |  |  | 			path:        "/api/", | 
					
						
							|  |  |  | 			expected:    "http://localhost:8080/api/", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for _, testcase := range tests { | 
					
						
							|  |  |  | 		t.Run("where "+testcase.description, func(t *testing.T) { | 
					
						
							|  |  |  | 			assert.Equalf( | 
					
						
							|  |  |  | 				t, | 
					
						
							|  |  |  | 				testcase.expected, | 
					
						
							|  |  |  | 				JoinURLFragments(testcase.base, testcase.path), | 
					
						
							|  |  |  | 				"base: '%s', path: '%s'", | 
					
						
							|  |  |  | 				testcase.base, | 
					
						
							|  |  |  | 				testcase.path, | 
					
						
							|  |  |  | 			) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-08-06 04:54:06 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-29 05:18:48 +08:00
										 |  |  | func TestNewURLQueryReader(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2020-08-13 17:10:48 +08:00
										 |  |  | 	u, err := url.Parse("http://www.abc.com/foo?bar=baz&bar2=baz2") | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2018-08-06 04:54:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-13 17:10:48 +08:00
										 |  |  | 	uqr, err := NewURLQueryReader(u) | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2018-08-06 04:54:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-13 17:10:48 +08:00
										 |  |  | 	assert.Equal(t, "baz", uqr.Get("bar", "foodef"), "first param") | 
					
						
							|  |  |  | 	assert.Equal(t, "baz2", uqr.Get("bar2", "foodef"), "second param") | 
					
						
							|  |  |  | 	assert.Equal(t, "foodef", uqr.Get("bar3", "foodef"), "non-existing param, use fallback") | 
					
						
							| 
									
										
										
										
											2016-01-30 01:31:00 +08:00
										 |  |  | } |