|
23 | 23 | import com.google.api.gax.grpc.GrpcCallContext; |
24 | 24 | import com.google.cloud.storage.BlobDescriptorState.OpenArguments; |
25 | 25 | import com.google.cloud.storage.BlobDescriptorStreamRead.AccumulatingRead; |
| 26 | +import com.google.common.collect.ImmutableList; |
| 27 | +import com.google.common.collect.ImmutableMap; |
26 | 28 | import com.google.protobuf.ByteString; |
27 | 29 | import com.google.storage.v2.BidiReadHandle; |
28 | 30 | import com.google.storage.v2.BidiReadObjectRequest; |
|
31 | 33 | import com.google.storage.v2.CommonObjectRequestParams; |
32 | 34 | import com.google.storage.v2.Object; |
33 | 35 | import com.google.storage.v2.ReadRange; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Map; |
34 | 38 | import org.junit.Test; |
35 | 39 |
|
36 | 40 | public final class BlobDescriptorStateTest { |
@@ -83,7 +87,11 @@ public void getOpenArguments_includesAllRelevantModifications() throws Exception |
83 | 87 |
|
84 | 88 | OpenArguments expected = |
85 | 89 | OpenArguments.of( |
86 | | - GrpcCallContext.createDefault(), |
| 90 | + GrpcCallContext.createDefault() |
| 91 | + .withExtraHeaders( |
| 92 | + ImmutableMap.of( |
| 93 | + "x-goog-request-params", |
| 94 | + ImmutableList.of("bucket=projects/_/buckets/my-bucket"))), |
87 | 95 | BidiReadObjectRequest.newBuilder() |
88 | 96 | .setReadObjectSpec( |
89 | 97 | BidiReadObjectSpec.newBuilder() |
@@ -111,4 +119,79 @@ public void getOpenArguments_includesAllRelevantModifications() throws Exception |
111 | 119 | assertThat(actual.getCtx().getExtraHeaders()) |
112 | 120 | .isEqualTo(expected.getCtx().getExtraHeaders())); |
113 | 121 | } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void redirectTokenPresentInHeadersIfNonNull() { |
| 125 | + BidiReadObjectRequest base = |
| 126 | + BidiReadObjectRequest.newBuilder() |
| 127 | + .setReadObjectSpec( |
| 128 | + BidiReadObjectSpec.newBuilder() |
| 129 | + .setBucket("projects/_/buckets/my-bucket") |
| 130 | + .setObject("my-object")) |
| 131 | + .build(); |
| 132 | + |
| 133 | + BlobDescriptorState state = new BlobDescriptorState(GrpcCallContext.createDefault(), base); |
| 134 | + |
| 135 | + state.setRoutingToken("token-1"); |
| 136 | + |
| 137 | + OpenArguments openArguments = state.getOpenArguments(); |
| 138 | + GrpcCallContext ctx = openArguments.getCtx(); |
| 139 | + Map<String, List<String>> extraHeaders = ctx.getExtraHeaders(); |
| 140 | + Map<String, List<String>> expected = |
| 141 | + ImmutableMap.of( |
| 142 | + "x-goog-request-params", |
| 143 | + ImmutableList.of("bucket=projects/_/buckets/my-bucket&routing_token=token-1")); |
| 144 | + |
| 145 | + assertThat(extraHeaders).isEqualTo(expected); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void redirectTokenNotPresentInHeadersIfNull() { |
| 150 | + BidiReadObjectRequest base = |
| 151 | + BidiReadObjectRequest.newBuilder() |
| 152 | + .setReadObjectSpec( |
| 153 | + BidiReadObjectSpec.newBuilder() |
| 154 | + .setBucket("projects/_/buckets/my-bucket") |
| 155 | + .setObject("my-object")) |
| 156 | + .build(); |
| 157 | + |
| 158 | + BlobDescriptorState state = new BlobDescriptorState(GrpcCallContext.createDefault(), base); |
| 159 | + |
| 160 | + state.setRoutingToken(null); |
| 161 | + |
| 162 | + OpenArguments openArguments = state.getOpenArguments(); |
| 163 | + GrpcCallContext ctx = openArguments.getCtx(); |
| 164 | + Map<String, List<String>> extraHeaders = ctx.getExtraHeaders(); |
| 165 | + Map<String, List<String>> expected = |
| 166 | + ImmutableMap.of( |
| 167 | + "x-goog-request-params", ImmutableList.of("bucket=projects/_/buckets/my-bucket")); |
| 168 | + |
| 169 | + assertThat(extraHeaders).isEqualTo(expected); |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + public void redirectTokenMustNotBeUrlEncoded() { |
| 174 | + BidiReadObjectRequest base = |
| 175 | + BidiReadObjectRequest.newBuilder() |
| 176 | + .setReadObjectSpec( |
| 177 | + BidiReadObjectSpec.newBuilder() |
| 178 | + .setBucket("projects/_/buckets/my-bucket") |
| 179 | + .setObject("my-object")) |
| 180 | + .build(); |
| 181 | + |
| 182 | + BlobDescriptorState state = new BlobDescriptorState(GrpcCallContext.createDefault(), base); |
| 183 | + |
| 184 | + state.setRoutingToken("token%20with%2furl%20encoding"); |
| 185 | + |
| 186 | + OpenArguments openArguments = state.getOpenArguments(); |
| 187 | + GrpcCallContext ctx = openArguments.getCtx(); |
| 188 | + Map<String, List<String>> extraHeaders = ctx.getExtraHeaders(); |
| 189 | + Map<String, List<String>> expected = |
| 190 | + ImmutableMap.of( |
| 191 | + "x-goog-request-params", |
| 192 | + ImmutableList.of( |
| 193 | + "bucket=projects/_/buckets/my-bucket&routing_token=token%20with%2furl%20encoding")); |
| 194 | + |
| 195 | + assertThat(extraHeaders).isEqualTo(expected); |
| 196 | + } |
114 | 197 | } |
0 commit comments