diff options
| author | Karun Agarwal <113603846+singhalkarun@users.noreply.github.com> | 2023-08-19 16:58:25 +0530 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-08-19 07:28:25 -0400 | 
| commit | 288216e1fbf25ebe11fcea7c71be526c4cd0dcce (patch) | |
| tree | d5218fe1f29c39f6c5f006b9706f4263f3d88fb3 /caddytest | |
| parent | 10053f7570b113d41c5a5ba40e6c457ab0784ef5 (diff) | |
httpcaddyfile: Stricter errors for site and upstream address schemes (#5757)
Co-authored-by: Mohammed Al Sahaf <msaa1990@gmail.com>
Co-authored-by: Francis Lavoie <lavofr@gmail.com>
Diffstat (limited to 'caddytest')
| -rw-r--r-- | caddytest/integration/caddyfile_test.go | 349 | 
1 files changed, 349 insertions, 0 deletions
| diff --git a/caddytest/integration/caddyfile_test.go b/caddytest/integration/caddyfile_test.go index 3f3ba0a..205bc5b 100644 --- a/caddytest/integration/caddyfile_test.go +++ b/caddytest/integration/caddyfile_test.go @@ -135,3 +135,352 @@ func TestReplIndex(t *testing.T) {  	// act and assert  	tester.AssertGetResponse("http://localhost:9080/", 200, "")  } + +func TestInvalidPrefix(t *testing.T) { +	type testCase struct { +		config, expectedError string +	} + +	failureCases := []testCase{ +		{ +			config:        `wss://localhost`, +			expectedError: `the scheme wss:// is only supported in browsers; use https:// instead`, +		}, +		{ +			config:        `ws://localhost`, +			expectedError: `the scheme ws:// is only supported in browsers; use http:// instead`, +		}, +		{ +			config:        `someInvalidPrefix://localhost`, +			expectedError: "unsupported URL scheme someinvalidprefix://", +		}, +		{ +			config:        `h2c://localhost`, +			expectedError: `unsupported URL scheme h2c://`, +		}, +		{ +			config:        `localhost, wss://localhost`, +			expectedError: `the scheme wss:// is only supported in browsers; use https:// instead`, +		}, +		{ +			config: `localhost { +  				reverse_proxy ws://localhost" +            }`, +			expectedError: `the scheme ws:// is only supported in browsers; use http:// instead`, +		}, +		{ +			config: `localhost { +  				reverse_proxy someInvalidPrefix://localhost" +			}`, +			expectedError: `unsupported URL scheme someinvalidprefix://`, +		}, +	} + +	for _, failureCase := range failureCases { +		caddytest.AssertLoadError(t, failureCase.config, "caddyfile", failureCase.expectedError) +	} +} + +func TestValidPrefix(t *testing.T) { +	type testCase struct { +		rawConfig, expectedResponse string +	} + +	successCases := []testCase{ +		{ +			"localhost", +			`{ +	"apps": { +		"http": { +			"servers": { +				"srv0": { +					"listen": [ +						":443" +					], +					"routes": [ +						{ +							"match": [ +								{ +									"host": [ +										"localhost" +									] +								} +							], +							"terminal": true +						} +					] +				} +			} +		} +	} +}`, +		}, +		{ +			"https://localhost", +			`{ +	"apps": { +		"http": { +			"servers": { +				"srv0": { +					"listen": [ +						":443" +					], +					"routes": [ +						{ +							"match": [ +								{ +									"host": [ +										"localhost" +									] +								} +							], +							"terminal": true +						} +					] +				} +			} +		} +	} +}`, +		}, +		{ +			"http://localhost", +			`{ +	"apps": { +		"http": { +			"servers": { +				"srv0": { +					"listen": [ +						":80" +					], +					"routes": [ +						{ +							"match": [ +								{ +									"host": [ +										"localhost" +									] +								} +							], +							"terminal": true +						} +					] +				} +			} +		} +	} +}`, +		}, +		{ +			`localhost { +			reverse_proxy http://localhost:3000 +		 }`, +			`{ +	"apps": { +		"http": { +			"servers": { +				"srv0": { +					"listen": [ +						":443" +					], +					"routes": [ +						{ +							"match": [ +								{ +									"host": [ +										"localhost" +									] +								} +							], +							"handle": [ +								{ +									"handler": "subroute", +									"routes": [ +										{ +											"handle": [ +												{ +													"handler": "reverse_proxy", +													"upstreams": [ +														{ +															"dial": "localhost:3000" +														} +													] +												} +											] +										} +									] +								} +							], +							"terminal": true +						} +					] +				} +			} +		} +	} +}`, +		}, +		{ +			`localhost { +			reverse_proxy https://localhost:3000 +		 }`, +			`{ +	"apps": { +		"http": { +			"servers": { +				"srv0": { +					"listen": [ +						":443" +					], +					"routes": [ +						{ +							"match": [ +								{ +									"host": [ +										"localhost" +									] +								} +							], +							"handle": [ +								{ +									"handler": "subroute", +									"routes": [ +										{ +											"handle": [ +												{ +													"handler": "reverse_proxy", +													"transport": { +														"protocol": "http", +														"tls": {} +													}, +													"upstreams": [ +														{ +															"dial": "localhost:3000" +														} +													] +												} +											] +										} +									] +								} +							], +							"terminal": true +						} +					] +				} +			} +		} +	} +}`, +		}, +		{ +			`localhost { +			reverse_proxy h2c://localhost:3000 +		 }`, +			`{ +	"apps": { +		"http": { +			"servers": { +				"srv0": { +					"listen": [ +						":443" +					], +					"routes": [ +						{ +							"match": [ +								{ +									"host": [ +										"localhost" +									] +								} +							], +							"handle": [ +								{ +									"handler": "subroute", +									"routes": [ +										{ +											"handle": [ +												{ +													"handler": "reverse_proxy", +													"transport": { +														"protocol": "http", +														"versions": [ +															"h2c", +															"2" +														] +													}, +													"upstreams": [ +														{ +															"dial": "localhost:3000" +														} +													] +												} +											] +										} +									] +								} +							], +							"terminal": true +						} +					] +				} +			} +		} +	} +}`, +		}, +		{ +			`localhost { +			reverse_proxy localhost:3000 +		 }`, +			`{ +	"apps": { +		"http": { +			"servers": { +				"srv0": { +					"listen": [ +						":443" +					], +					"routes": [ +						{ +							"match": [ +								{ +									"host": [ +										"localhost" +									] +								} +							], +							"handle": [ +								{ +									"handler": "subroute", +									"routes": [ +										{ +											"handle": [ +												{ +													"handler": "reverse_proxy", +													"upstreams": [ +														{ +															"dial": "localhost:3000" +														} +													] +												} +											] +										} +									] +								} +							], +							"terminal": true +						} +					] +				} +			} +		} +	} +}`, +		}, +	} + +	for _, successCase := range successCases { +		caddytest.AssertAdapt(t, successCase.rawConfig, "caddyfile", successCase.expectedResponse) +	} +} | 
