|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +import json |
15 | 16 | import pytest |
| 17 | +from google.protobuf import descriptor_pb2 |
16 | 18 |
|
17 | 19 | from google.api_core import rest_helpers |
| 20 | +from google.api_core.rest_helpers import transcode |
18 | 21 |
|
19 | 22 |
|
20 | 23 | def test_flatten_simple_value(): |
@@ -92,3 +95,120 @@ def test_flatten_repeated_list(): |
92 | 95 |
|
93 | 96 | with pytest.raises(ValueError): |
94 | 97 | rest_helpers.flatten_query_params(obj) |
| 98 | + |
| 99 | + |
| 100 | +def test_transcode_basic(): |
| 101 | + # We use FieldDescriptorProto as it has standard primitive fields and nested messages. |
| 102 | + http_options = [ |
| 103 | + { |
| 104 | + "method": "get", |
| 105 | + "uri": "/v1/test/{name}", |
| 106 | + } |
| 107 | + ] |
| 108 | + |
| 109 | + request = descriptor_pb2.FieldDescriptorProto() |
| 110 | + request.name = "my-field" |
| 111 | + request.number = 123 |
| 112 | + |
| 113 | + transcoded, body, query_params = transcode(http_options, request) |
| 114 | + |
| 115 | + assert transcoded["method"] == "get" |
| 116 | + assert transcoded["uri"] == "/v1/test/my-field" |
| 117 | + assert body is None |
| 118 | + # 'number' should be in query parameters |
| 119 | + assert "number" in query_params |
| 120 | + assert query_params["number"] == 123 |
| 121 | + |
| 122 | + |
| 123 | +def test_transcode_with_nested_field(): |
| 124 | + http_options = [ |
| 125 | + { |
| 126 | + "method": "get", |
| 127 | + "uri": "/v1/test/{options.deprecated}/{name}", |
| 128 | + } |
| 129 | + ] |
| 130 | + |
| 131 | + request = descriptor_pb2.FieldDescriptorProto() |
| 132 | + request.name = "my-field" |
| 133 | + request.options.deprecated = True |
| 134 | + request.number = 123 |
| 135 | + |
| 136 | + transcoded, body, query_params = transcode(http_options, request) |
| 137 | + |
| 138 | + assert transcoded["method"] == "get" |
| 139 | + assert transcoded["uri"] == "/v1/test/True/my-field" |
| 140 | + assert body is None |
| 141 | + assert "number" in query_params |
| 142 | + assert query_params["number"] == 123 |
| 143 | + |
| 144 | + |
| 145 | +def test_transcode_with_body(): |
| 146 | + http_options = [ |
| 147 | + { |
| 148 | + "method": "post", |
| 149 | + "uri": "/v1/test/{name}", |
| 150 | + "body": "options", |
| 151 | + } |
| 152 | + ] |
| 153 | + |
| 154 | + request = descriptor_pb2.FieldDescriptorProto() |
| 155 | + request.name = "my-field" |
| 156 | + request.options.deprecated = True |
| 157 | + request.number = 123 |
| 158 | + |
| 159 | + transcoded, body, query_params = transcode(http_options, request) |
| 160 | + |
| 161 | + assert transcoded["method"] == "post" |
| 162 | + assert transcoded["uri"] == "/v1/test/my-field" |
| 163 | + assert body is not None |
| 164 | + body_data = json.loads(body) |
| 165 | + assert body_data["deprecated"] is True |
| 166 | + # Query parameters should not contain 'options' (the body) |
| 167 | + assert "number" in query_params |
| 168 | + assert query_params["number"] == 123 |
| 169 | + assert "options" not in query_params |
| 170 | + |
| 171 | + |
| 172 | +def test_transcode_with_required_fields_default_values(): |
| 173 | + http_options = [ |
| 174 | + { |
| 175 | + "method": "get", |
| 176 | + "uri": "/v1/test/{name}", |
| 177 | + } |
| 178 | + ] |
| 179 | + |
| 180 | + request = descriptor_pb2.FieldDescriptorProto() |
| 181 | + request.name = "my-field" |
| 182 | + |
| 183 | + required_defaults = {"requiredQueryParam": "default-val"} |
| 184 | + |
| 185 | + transcoded, body, query_params = transcode( |
| 186 | + http_options, |
| 187 | + request, |
| 188 | + required_fields_default_values=required_defaults, |
| 189 | + ) |
| 190 | + |
| 191 | + assert query_params["requiredQueryParam"] == "default-val" |
| 192 | + |
| 193 | + |
| 194 | +def test_transcode_with_numeric_enums(): |
| 195 | + http_options = [ |
| 196 | + { |
| 197 | + "method": "get", |
| 198 | + "uri": "/v1/test/{name}", |
| 199 | + } |
| 200 | + ] |
| 201 | + |
| 202 | + request = descriptor_pb2.FieldDescriptorProto() |
| 203 | + request.name = "my-field" |
| 204 | + request.type = descriptor_pb2.FieldDescriptorProto.TYPE_STRING |
| 205 | + |
| 206 | + # Without numeric enums |
| 207 | + _, _, query_params = transcode(http_options, request, rest_numeric_enums=False) |
| 208 | + assert query_params["type"] == "TYPE_STRING" |
| 209 | + |
| 210 | + # With numeric enums |
| 211 | + _, _, query_params = transcode(http_options, request, rest_numeric_enums=True) |
| 212 | + # Type number for TYPE_STRING is 9 |
| 213 | + assert query_params["type"] == 9 |
| 214 | + assert query_params["$alt"] == "json;enum-encoding=int" |
0 commit comments