|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import json |
| 16 | + |
| 17 | +from google.protobuf import descriptor_pb2 |
| 18 | + |
| 19 | +from google.api_core.gapic_v1.rest_helpers import transcode |
| 20 | + |
| 21 | + |
| 22 | +def test_transcode_basic(): |
| 23 | + # We use FieldDescriptorProto as it has standard primitive fields and nested messages. |
| 24 | + http_options = [ |
| 25 | + { |
| 26 | + "method": "get", |
| 27 | + "uri": "/v1/test/{name}", |
| 28 | + } |
| 29 | + ] |
| 30 | + |
| 31 | + request = descriptor_pb2.FieldDescriptorProto() |
| 32 | + request.name = "my-field" |
| 33 | + request.number = 123 |
| 34 | + |
| 35 | + transcoded, body, query_params = transcode(http_options, request) |
| 36 | + |
| 37 | + assert transcoded["method"] == "get" |
| 38 | + assert transcoded["uri"] == "/v1/test/my-field" |
| 39 | + assert body is None |
| 40 | + # 'number' should be in query parameters |
| 41 | + assert "number" in query_params |
| 42 | + assert query_params["number"] == 123 |
| 43 | + |
| 44 | + |
| 45 | +def test_transcode_with_nested_field(): |
| 46 | + http_options = [ |
| 47 | + { |
| 48 | + "method": "get", |
| 49 | + "uri": "/v1/test/{options.deprecated}/{name}", |
| 50 | + } |
| 51 | + ] |
| 52 | + |
| 53 | + request = descriptor_pb2.FieldDescriptorProto() |
| 54 | + request.name = "my-field" |
| 55 | + request.options.deprecated = True |
| 56 | + request.number = 123 |
| 57 | + |
| 58 | + transcoded, body, query_params = transcode(http_options, request) |
| 59 | + |
| 60 | + assert transcoded["method"] == "get" |
| 61 | + assert transcoded["uri"] == "/v1/test/True/my-field" |
| 62 | + assert body is None |
| 63 | + assert "number" in query_params |
| 64 | + assert query_params["number"] == 123 |
| 65 | + |
| 66 | + |
| 67 | +def test_transcode_with_body(): |
| 68 | + http_options = [ |
| 69 | + { |
| 70 | + "method": "post", |
| 71 | + "uri": "/v1/test/{name}", |
| 72 | + "body": "options", |
| 73 | + } |
| 74 | + ] |
| 75 | + |
| 76 | + request = descriptor_pb2.FieldDescriptorProto() |
| 77 | + request.name = "my-field" |
| 78 | + request.options.deprecated = True |
| 79 | + request.number = 123 |
| 80 | + |
| 81 | + transcoded, body, query_params = transcode(http_options, request) |
| 82 | + |
| 83 | + assert transcoded["method"] == "post" |
| 84 | + assert transcoded["uri"] == "/v1/test/my-field" |
| 85 | + assert body is not None |
| 86 | + body_data = json.loads(body) |
| 87 | + assert body_data["deprecated"] is True |
| 88 | + # Query parameters should not contain 'options' (the body) |
| 89 | + assert "number" in query_params |
| 90 | + assert query_params["number"] == 123 |
| 91 | + assert "options" not in query_params |
| 92 | + |
| 93 | + |
| 94 | +def test_transcode_with_required_fields_default_values(): |
| 95 | + http_options = [ |
| 96 | + { |
| 97 | + "method": "get", |
| 98 | + "uri": "/v1/test/{name}", |
| 99 | + } |
| 100 | + ] |
| 101 | + |
| 102 | + request = descriptor_pb2.FieldDescriptorProto() |
| 103 | + request.name = "my-field" |
| 104 | + |
| 105 | + required_defaults = {"requiredQueryParam": "default-val"} |
| 106 | + |
| 107 | + transcoded, body, query_params = transcode( |
| 108 | + http_options, |
| 109 | + request, |
| 110 | + required_fields_default_values=required_defaults, |
| 111 | + ) |
| 112 | + |
| 113 | + assert query_params["requiredQueryParam"] == "default-val" |
| 114 | + |
| 115 | + |
| 116 | +def test_transcode_with_numeric_enums(): |
| 117 | + http_options = [ |
| 118 | + { |
| 119 | + "method": "get", |
| 120 | + "uri": "/v1/test/{name}", |
| 121 | + } |
| 122 | + ] |
| 123 | + |
| 124 | + request = descriptor_pb2.FieldDescriptorProto() |
| 125 | + request.name = "my-field" |
| 126 | + request.type = descriptor_pb2.FieldDescriptorProto.TYPE_STRING |
| 127 | + |
| 128 | + # Without numeric enums |
| 129 | + _, _, query_params = transcode(http_options, request, rest_numeric_enums=False) |
| 130 | + assert query_params["type"] == "TYPE_STRING" |
| 131 | + |
| 132 | + # With numeric enums |
| 133 | + _, _, query_params = transcode(http_options, request, rest_numeric_enums=True) |
| 134 | + # Type number for TYPE_STRING is 9 |
| 135 | + assert query_params["type"] == 9 |
| 136 | + assert query_params["$alt"] == "json;enum-encoding=int" |
0 commit comments