|
16 | 16 | import com.google.api.client.googleapis.testing.services.MockGoogleClient;
|
17 | 17 | import com.google.api.client.googleapis.testing.services.MockGoogleClientRequest;
|
18 | 18 | import com.google.api.client.http.EmptyContent;
|
| 19 | +import com.google.api.client.http.GenericUrl; |
19 | 20 | import com.google.api.client.http.HttpExecuteInterceptor;
|
20 | 21 | import com.google.api.client.http.HttpRequest;
|
21 | 22 | import com.google.api.client.http.HttpRequestInitializer;
|
|
33 | 34 | import com.google.api.client.util.Key;
|
34 | 35 | import java.io.ByteArrayInputStream;
|
35 | 36 | import java.io.InputStream;
|
| 37 | +import java.util.ArrayList; |
| 38 | +import java.util.List; |
36 | 39 | import junit.framework.TestCase;
|
37 | 40 |
|
38 | 41 | /**
|
@@ -132,6 +135,7 @@ private static class MediaTransport extends MockHttpTransport {
|
132 | 135 | int bytesUploaded;
|
133 | 136 | int contentLength = MediaHttpUploader.DEFAULT_CHUNK_SIZE;
|
134 | 137 | boolean contentLengthNotSpecified;
|
| 138 | + List<String> userAgentsRecorded = new ArrayList<>(); |
135 | 139 |
|
136 | 140 | protected MediaTransport() {}
|
137 | 141 |
|
@@ -169,6 +173,7 @@ public LowLevelHttpResponse execute() {
|
169 | 173 | String expectedContentRange = "bytes " + bytesRange + "/" + contentLength;
|
170 | 174 | assertEquals(expectedContentRange, getFirstHeaderValue("Content-Range"));
|
171 | 175 | bytesUploaded += MediaHttpUploader.DEFAULT_CHUNK_SIZE;
|
| 176 | + userAgentsRecorded.add(getFirstHeaderValue("User-Agent")); |
172 | 177 |
|
173 | 178 | if (bytesUploaded == contentLength) {
|
174 | 179 | // Return 200 since the upload is complete.
|
@@ -207,6 +212,32 @@ public void testMediaUpload() throws Exception {
|
207 | 212 | assertEquals("somevalue", result.foo);
|
208 | 213 | }
|
209 | 214 |
|
| 215 | + public void testMediaUpload_applicationNameAsUserAgent() throws Exception { |
| 216 | + MediaTransport fakeTransport = new MediaTransport(); |
| 217 | + String applicationName = "Foo/1.0 (BAR:Baz/1.0) XYZ/1.0"; |
| 218 | + AbstractGoogleClient client = |
| 219 | + new MockGoogleClient.Builder( |
| 220 | + fakeTransport, TEST_RESUMABLE_REQUEST_URL, "", JSON_OBJECT_PARSER, null) |
| 221 | + .setApplicationName(applicationName) |
| 222 | + .build(); |
| 223 | + InputStream is = new ByteArrayInputStream(new byte[MediaHttpUploader.DEFAULT_CHUNK_SIZE]); |
| 224 | + InputStreamContent mediaContent = new InputStreamContent(TEST_CONTENT_TYPE, is); |
| 225 | + mediaContent.setLength(MediaHttpUploader.DEFAULT_CHUNK_SIZE); |
| 226 | + MockGoogleClientRequest<A> rq = |
| 227 | + new MockGoogleClientRequest<A>(client, "POST", "", null, A.class); |
| 228 | + |
| 229 | + rq.initializeMediaUpload(mediaContent); |
| 230 | + MediaHttpUploader mediaHttpUploader = rq.getMediaHttpUploader(); |
| 231 | + mediaHttpUploader.upload(new GenericUrl(TEST_RESUMABLE_REQUEST_URL)); |
| 232 | + |
| 233 | + assertEquals(1, fakeTransport.userAgentsRecorded.size()); |
| 234 | + for (String userAgent : fakeTransport.userAgentsRecorded) { |
| 235 | + assertTrue( |
| 236 | + "UserAgent header does not have expected value in requests", |
| 237 | + userAgent.contains(applicationName)); |
| 238 | + } |
| 239 | + } |
| 240 | + |
210 | 241 | private static class GZipCheckerInitializer implements HttpRequestInitializer {
|
211 | 242 |
|
212 | 243 | private boolean gzipDisabled;
|
|
0 commit comments