| 
									
										
										
										
											2022-02-08 20:12:51 -01:00
										 |  |  | import boto3 | 
					
						
							|  |  |  | import pytest | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from botocore.exceptions import ClientError | 
					
						
							|  |  |  | from moto import mock_apigatewayv2 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_apigatewayv2 | 
					
						
							|  |  |  | def test_get_routes_empty(): | 
					
						
							|  |  |  |     client = boto3.client("apigatewayv2", region_name="eu-west-1") | 
					
						
							|  |  |  |     api_id = client.create_api(Name="test-api", ProtocolType="HTTP")["ApiId"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = client.get_routes(ApiId=api_id) | 
					
						
							| 
									
										
										
										
											2023-06-08 11:40:10 +00:00
										 |  |  |     assert resp["Items"] == [] | 
					
						
							| 
									
										
										
										
											2022-02-08 20:12:51 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_apigatewayv2 | 
					
						
							|  |  |  | def test_create_route_minimal(): | 
					
						
							|  |  |  |     client = boto3.client("apigatewayv2", region_name="ap-southeast-1") | 
					
						
							|  |  |  |     api_id = client.create_api(Name="test-api", ProtocolType="HTTP")["ApiId"] | 
					
						
							|  |  |  |     resp = client.create_route(ApiId=api_id, RouteKey="GET /") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-08 11:40:10 +00:00
										 |  |  |     assert resp["ApiKeyRequired"] is False | 
					
						
							|  |  |  |     assert resp["AuthorizationType"] == "NONE" | 
					
						
							|  |  |  |     assert "RouteId" in resp | 
					
						
							|  |  |  |     assert resp["RouteKey"] == "GET /" | 
					
						
							| 
									
										
										
										
											2022-02-08 20:12:51 -01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-08 11:40:10 +00:00
										 |  |  |     assert "AuthorizationScopes" not in resp | 
					
						
							|  |  |  |     assert "AuthorizerId" not in resp | 
					
						
							|  |  |  |     assert "ModelSelectionExpression" not in resp | 
					
						
							|  |  |  |     assert "OperationName" not in resp | 
					
						
							|  |  |  |     assert "RequestModels" not in resp | 
					
						
							|  |  |  |     assert "RouteResponseSelectionExpression" not in resp | 
					
						
							|  |  |  |     assert "Target" not in resp | 
					
						
							| 
									
										
										
										
											2022-02-08 20:12:51 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_apigatewayv2 | 
					
						
							|  |  |  | def test_create_route_full(): | 
					
						
							|  |  |  |     client = boto3.client("apigatewayv2", region_name="ap-southeast-1") | 
					
						
							|  |  |  |     api_id = client.create_api( | 
					
						
							|  |  |  |         Name="test-api", | 
					
						
							|  |  |  |         ProtocolType="WEBSOCKET", | 
					
						
							|  |  |  |         RouteSelectionExpression="${request.method}", | 
					
						
							|  |  |  |     )["ApiId"] | 
					
						
							|  |  |  |     resp = client.create_route( | 
					
						
							|  |  |  |         ApiId=api_id, | 
					
						
							|  |  |  |         ApiKeyRequired=True, | 
					
						
							|  |  |  |         AuthorizerId="auth_id", | 
					
						
							|  |  |  |         AuthorizationScopes=["scope1", "scope2"], | 
					
						
							|  |  |  |         AuthorizationType="CUSTOM", | 
					
						
							|  |  |  |         ModelSelectionExpression="mse", | 
					
						
							|  |  |  |         OperationName="OP", | 
					
						
							|  |  |  |         RequestModels={"req": "uest"}, | 
					
						
							|  |  |  |         RequestParameters={"action": {"Required": True}}, | 
					
						
							|  |  |  |         RouteKey="GET /", | 
					
						
							|  |  |  |         RouteResponseSelectionExpression="$default", | 
					
						
							|  |  |  |         Target="t", | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-08 11:40:10 +00:00
										 |  |  |     assert resp["ApiKeyRequired"] is True | 
					
						
							|  |  |  |     assert resp["AuthorizationType"] == "CUSTOM" | 
					
						
							|  |  |  |     assert resp["AuthorizationScopes"] == ["scope1", "scope2"] | 
					
						
							|  |  |  |     assert resp["AuthorizerId"] == "auth_id" | 
					
						
							|  |  |  |     assert "RouteId" in resp | 
					
						
							|  |  |  |     assert resp["RouteKey"] == "GET /" | 
					
						
							| 
									
										
										
										
											2022-02-08 20:12:51 -01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-08 11:40:10 +00:00
										 |  |  |     assert resp["ModelSelectionExpression"] == "mse" | 
					
						
							|  |  |  |     assert resp["OperationName"] == "OP" | 
					
						
							|  |  |  |     assert resp["RequestModels"] == {"req": "uest"} | 
					
						
							|  |  |  |     assert resp["RequestParameters"] == {"action": {"Required": True}} | 
					
						
							|  |  |  |     assert resp["RouteResponseSelectionExpression"] == "$default" | 
					
						
							|  |  |  |     assert resp["Target"] == "t" | 
					
						
							| 
									
										
										
										
											2022-02-08 20:12:51 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_apigatewayv2 | 
					
						
							|  |  |  | def test_delete_route(): | 
					
						
							|  |  |  |     client = boto3.client("apigatewayv2", region_name="eu-west-1") | 
					
						
							|  |  |  |     api_id = client.create_api(Name="test-api", ProtocolType="HTTP")["ApiId"] | 
					
						
							|  |  |  |     route_id = client.create_route(ApiId=api_id, RouteKey="GET /")["RouteId"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     client.delete_route(ApiId=api_id, RouteId=route_id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = client.get_routes(ApiId=api_id) | 
					
						
							| 
									
										
										
										
											2023-06-08 11:40:10 +00:00
										 |  |  |     assert len(resp["Items"]) == 0 | 
					
						
							| 
									
										
										
										
											2022-02-08 20:12:51 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_apigatewayv2 | 
					
						
							|  |  |  | def test_get_route(): | 
					
						
							|  |  |  |     client = boto3.client("apigatewayv2", region_name="ap-southeast-1") | 
					
						
							|  |  |  |     api_id = client.create_api(Name="test-api", ProtocolType="HTTP")["ApiId"] | 
					
						
							|  |  |  |     route_id = client.create_route(ApiId=api_id, RouteKey="GET /")["RouteId"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = client.get_route(ApiId=api_id, RouteId=route_id) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-08 11:40:10 +00:00
										 |  |  |     assert resp["ApiKeyRequired"] is False | 
					
						
							|  |  |  |     assert resp["AuthorizationType"] == "NONE" | 
					
						
							|  |  |  |     assert "RouteId" in resp | 
					
						
							|  |  |  |     assert resp["RouteKey"] == "GET /" | 
					
						
							| 
									
										
										
										
											2022-02-08 20:12:51 -01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-08 11:40:10 +00:00
										 |  |  |     assert "AuthorizationScopes" not in resp | 
					
						
							|  |  |  |     assert "AuthorizerId" not in resp | 
					
						
							|  |  |  |     assert "ModelSelectionExpression" not in resp | 
					
						
							|  |  |  |     assert "OperationName" not in resp | 
					
						
							|  |  |  |     assert "RouteResponseSelectionExpression" not in resp | 
					
						
							|  |  |  |     assert "Target" not in resp | 
					
						
							| 
									
										
										
										
											2022-02-08 20:12:51 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_apigatewayv2 | 
					
						
							|  |  |  | def test_get_route_unknown(): | 
					
						
							|  |  |  |     client = boto3.client("apigatewayv2", region_name="ap-southeast-1") | 
					
						
							|  |  |  |     api_id = client.create_api(Name="test-api", ProtocolType="HTTP")["ApiId"] | 
					
						
							|  |  |  |     with pytest.raises(ClientError) as exc: | 
					
						
							|  |  |  |         client.get_route(ApiId=api_id, RouteId="unknown") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     err = exc.value.response["Error"] | 
					
						
							| 
									
										
										
										
											2023-06-08 11:40:10 +00:00
										 |  |  |     assert err["Code"] == "NotFoundException" | 
					
						
							|  |  |  |     assert err["Message"] == "Invalid Route identifier specified unknown" | 
					
						
							| 
									
										
										
										
											2022-02-08 20:12:51 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_apigatewayv2 | 
					
						
							|  |  |  | def test_get_routes(): | 
					
						
							|  |  |  |     client = boto3.client("apigatewayv2", region_name="eu-west-1") | 
					
						
							|  |  |  |     api_id = client.create_api(Name="test-api", ProtocolType="HTTP")["ApiId"] | 
					
						
							|  |  |  |     client.create_route(ApiId=api_id, RouteKey="GET /") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = client.get_routes(ApiId=api_id) | 
					
						
							| 
									
										
										
										
											2023-06-08 11:40:10 +00:00
										 |  |  |     assert len(resp["Items"]) == 1 | 
					
						
							| 
									
										
										
										
											2022-02-08 20:12:51 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_apigatewayv2 | 
					
						
							|  |  |  | def test_update_route_single_attribute(): | 
					
						
							|  |  |  |     client = boto3.client("apigatewayv2", region_name="ap-southeast-1") | 
					
						
							|  |  |  |     api_id = client.create_api(Name="test-api", ProtocolType="HTTP")["ApiId"] | 
					
						
							|  |  |  |     route_id = client.create_route(ApiId=api_id, RouteKey="GET /")["RouteId"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = client.update_route(ApiId=api_id, RouteId=route_id, RouteKey="POST /") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-08 11:40:10 +00:00
										 |  |  |     assert resp["ApiKeyRequired"] is False | 
					
						
							|  |  |  |     assert resp["AuthorizationType"] == "NONE" | 
					
						
							|  |  |  |     assert resp["RouteId"] == route_id | 
					
						
							|  |  |  |     assert resp["RouteKey"] == "POST /" | 
					
						
							| 
									
										
										
										
											2022-02-08 20:12:51 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_apigatewayv2 | 
					
						
							|  |  |  | def test_update_route_all_attributes(): | 
					
						
							|  |  |  |     client = boto3.client("apigatewayv2", region_name="ap-southeast-1") | 
					
						
							|  |  |  |     api_id = client.create_api(Name="test-api", ProtocolType="HTTP")["ApiId"] | 
					
						
							|  |  |  |     route_id = client.create_route(ApiId=api_id, ApiKeyRequired=True, RouteKey="GET /")[ | 
					
						
							|  |  |  |         "RouteId" | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = client.update_route( | 
					
						
							|  |  |  |         ApiId=api_id, | 
					
						
							|  |  |  |         RouteId=route_id, | 
					
						
							|  |  |  |         ApiKeyRequired=False, | 
					
						
							|  |  |  |         AuthorizationScopes=["scope"], | 
					
						
							|  |  |  |         AuthorizerId="auth_id", | 
					
						
							|  |  |  |         AuthorizationType="JWT", | 
					
						
							|  |  |  |         ModelSelectionExpression="mse", | 
					
						
							|  |  |  |         OperationName="OP", | 
					
						
							|  |  |  |         RequestModels={"req": "uest"}, | 
					
						
							|  |  |  |         RequestParameters={"action": {"Required": True}}, | 
					
						
							|  |  |  |         RouteResponseSelectionExpression="$default", | 
					
						
							|  |  |  |         Target="t", | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-08 11:40:10 +00:00
										 |  |  |     assert resp["ApiKeyRequired"] is False | 
					
						
							|  |  |  |     assert resp["AuthorizationType"] == "JWT" | 
					
						
							|  |  |  |     assert resp["AuthorizationScopes"] == ["scope"] | 
					
						
							|  |  |  |     assert resp["AuthorizerId"] == "auth_id" | 
					
						
							|  |  |  |     assert "RouteId" in resp | 
					
						
							|  |  |  |     assert resp["RouteKey"] == "GET /" | 
					
						
							|  |  |  |     assert resp["ModelSelectionExpression"] == "mse" | 
					
						
							|  |  |  |     assert resp["OperationName"] == "OP" | 
					
						
							|  |  |  |     assert resp["RequestModels"] == {"req": "uest"} | 
					
						
							|  |  |  |     assert resp["RequestParameters"] == {"action": {"Required": True}} | 
					
						
							|  |  |  |     assert resp["RouteResponseSelectionExpression"] == "$default" | 
					
						
							|  |  |  |     assert resp["Target"] == "t" | 
					
						
							| 
									
										
										
										
											2022-02-08 20:12:51 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_apigatewayv2 | 
					
						
							|  |  |  | def test_delete_route_request_parameter(): | 
					
						
							|  |  |  |     client = boto3.client("apigatewayv2", region_name="us-east-1") | 
					
						
							|  |  |  |     api_id = client.create_api(Name="test-api", ProtocolType="HTTP")["ApiId"] | 
					
						
							|  |  |  |     route_id = client.create_route( | 
					
						
							|  |  |  |         ApiId=api_id, | 
					
						
							|  |  |  |         RequestParameters={ | 
					
						
							|  |  |  |             "action": {"Required": True}, | 
					
						
							|  |  |  |             "route.request.header.authorization": {"Required": False}, | 
					
						
							|  |  |  |             "zparam": {"Required": False}, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         RouteKey="GET /", | 
					
						
							|  |  |  |     )["RouteId"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     request_params = client.get_route(ApiId=api_id, RouteId=route_id)[ | 
					
						
							|  |  |  |         "RequestParameters" | 
					
						
							|  |  |  |     ] | 
					
						
							| 
									
										
										
										
											2023-06-08 11:40:10 +00:00
										 |  |  |     assert len(request_params.keys()) == 3 | 
					
						
							| 
									
										
										
										
											2022-02-08 20:12:51 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     client.delete_route_request_parameter( | 
					
						
							|  |  |  |         ApiId=api_id, | 
					
						
							|  |  |  |         RouteId=route_id, | 
					
						
							|  |  |  |         RequestParameterKey="route.request.header.authorization", | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     request_params = client.get_route(ApiId=api_id, RouteId=route_id)[ | 
					
						
							|  |  |  |         "RequestParameters" | 
					
						
							|  |  |  |     ] | 
					
						
							| 
									
										
										
										
											2023-06-08 11:40:10 +00:00
										 |  |  |     assert len(request_params.keys()) == 2 | 
					
						
							|  |  |  |     assert "action" in request_params | 
					
						
							|  |  |  |     assert "zparam" in request_params | 
					
						
							| 
									
										
										
										
											2022-02-08 20:12:51 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_apigatewayv2 | 
					
						
							|  |  |  | def test_create_route_response_minimal(): | 
					
						
							|  |  |  |     client = boto3.client("apigatewayv2", region_name="ap-southeast-1") | 
					
						
							|  |  |  |     api_id = client.create_api(Name="test-api", ProtocolType="HTTP")["ApiId"] | 
					
						
							|  |  |  |     route_id = client.create_route(ApiId=api_id, RouteKey="GET /")["RouteId"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = client.create_route_response( | 
					
						
							|  |  |  |         ApiId=api_id, RouteId=route_id, RouteResponseKey="$default" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-08 11:40:10 +00:00
										 |  |  |     assert "RouteResponseId" in resp | 
					
						
							|  |  |  |     assert resp["RouteResponseKey"] == "$default" | 
					
						
							| 
									
										
										
										
											2022-02-08 20:12:51 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_apigatewayv2 | 
					
						
							|  |  |  | def test_create_route_response(): | 
					
						
							|  |  |  |     client = boto3.client("apigatewayv2", region_name="ap-southeast-1") | 
					
						
							|  |  |  |     api_id = client.create_api(Name="test-api", ProtocolType="HTTP")["ApiId"] | 
					
						
							|  |  |  |     route_id = client.create_route(ApiId=api_id, RouteKey="GET /")["RouteId"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = client.create_route_response( | 
					
						
							|  |  |  |         ApiId=api_id, | 
					
						
							|  |  |  |         ModelSelectionExpression="mse", | 
					
						
							|  |  |  |         ResponseModels={"test": "tfacctest5832545056931060873"}, | 
					
						
							|  |  |  |         RouteId=route_id, | 
					
						
							|  |  |  |         RouteResponseKey="$default", | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-08 11:40:10 +00:00
										 |  |  |     assert "RouteResponseId" in resp | 
					
						
							|  |  |  |     assert resp["RouteResponseKey"] == "$default" | 
					
						
							|  |  |  |     assert resp["ModelSelectionExpression"] == "mse" | 
					
						
							|  |  |  |     assert resp["ResponseModels"] == {"test": "tfacctest5832545056931060873"} | 
					
						
							| 
									
										
										
										
											2022-02-08 20:12:51 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_apigatewayv2 | 
					
						
							|  |  |  | def test_get_route_response(): | 
					
						
							|  |  |  |     client = boto3.client("apigatewayv2", region_name="ap-southeast-1") | 
					
						
							|  |  |  |     api_id = client.create_api(Name="test-api", ProtocolType="HTTP")["ApiId"] | 
					
						
							|  |  |  |     route_id = client.create_route(ApiId=api_id, RouteKey="GET /")["RouteId"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     route_response_id = client.create_route_response( | 
					
						
							|  |  |  |         ApiId=api_id, RouteId=route_id, RouteResponseKey="$default" | 
					
						
							|  |  |  |     )["RouteResponseId"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = client.get_route_response( | 
					
						
							|  |  |  |         ApiId=api_id, RouteId=route_id, RouteResponseId=route_response_id | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-08 11:40:10 +00:00
										 |  |  |     assert "RouteResponseId" in resp | 
					
						
							|  |  |  |     assert resp["RouteResponseKey"] == "$default" | 
					
						
							| 
									
										
										
										
											2022-02-08 20:12:51 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_apigatewayv2 | 
					
						
							|  |  |  | def test_get_route_response_unknown(): | 
					
						
							|  |  |  |     client = boto3.client("apigatewayv2", region_name="ap-southeast-1") | 
					
						
							|  |  |  |     api_id = client.create_api(Name="test-api", ProtocolType="HTTP")["ApiId"] | 
					
						
							|  |  |  |     route_id = client.create_route(ApiId=api_id, RouteKey="GET /")["RouteId"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with pytest.raises(ClientError) as exc: | 
					
						
							|  |  |  |         client.get_route_response( | 
					
						
							|  |  |  |             ApiId=api_id, RouteId=route_id, RouteResponseId="unknown" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     err = exc.value.response["Error"] | 
					
						
							| 
									
										
										
										
											2023-06-08 11:40:10 +00:00
										 |  |  |     assert err["Code"] == "NotFoundException" | 
					
						
							| 
									
										
										
										
											2022-02-08 20:12:51 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_apigatewayv2 | 
					
						
							|  |  |  | def test_delete_route_response_unknown(): | 
					
						
							|  |  |  |     client = boto3.client("apigatewayv2", region_name="ap-southeast-1") | 
					
						
							|  |  |  |     api_id = client.create_api(Name="test-api", ProtocolType="HTTP")["ApiId"] | 
					
						
							|  |  |  |     r_id = client.create_route(ApiId=api_id, RouteKey="GET /")["RouteId"] | 
					
						
							|  |  |  |     rr_id = client.create_route_response( | 
					
						
							|  |  |  |         ApiId=api_id, RouteId=r_id, RouteResponseKey="$default" | 
					
						
							|  |  |  |     )["RouteResponseId"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     client.delete_route_response(ApiId=api_id, RouteId=r_id, RouteResponseId=rr_id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with pytest.raises(ClientError) as exc: | 
					
						
							|  |  |  |         client.get_route_response(ApiId=api_id, RouteId=r_id, RouteResponseId=rr_id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     err = exc.value.response["Error"] | 
					
						
							| 
									
										
										
										
											2023-06-08 11:40:10 +00:00
										 |  |  |     assert err["Code"] == "NotFoundException" |