Skip to content

Commit 172dfb8

Browse files
committed
Allow PATCH as a valid initial method for uploads
1 parent 32e2128 commit 172dfb8

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

google-api-client/src/main/java/com/google/api/client/googleapis/media/MediaHttpUploader.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,8 @@ public String getInitiationRequestMethod() {
870870
*/
871871
public MediaHttpUploader setInitiationRequestMethod(String initiationRequestMethod) {
872872
Preconditions.checkArgument(initiationRequestMethod.equals(HttpMethods.POST)
873-
|| initiationRequestMethod.equals(HttpMethods.PUT));
873+
|| initiationRequestMethod.equals(HttpMethods.PUT)
874+
|| initiationRequestMethod.equals(HttpMethods.PATCH));
874875
this.initiationRequestMethod = initiationRequestMethod;
875876
return this;
876877
}

google-api-client/src/test/java/com/google/api/client/googleapis/media/MediaHttpUploaderTest.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.api.client.http.HttpBackOffIOExceptionHandler;
2222
import com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler;
2323
import com.google.api.client.http.HttpExecuteInterceptor;
24+
import com.google.api.client.http.HttpMethods;
2425
import com.google.api.client.http.HttpRequest;
2526
import com.google.api.client.http.HttpRequestInitializer;
2627
import com.google.api.client.http.HttpResponse;
@@ -101,6 +102,7 @@ static class MediaTransport extends MockHttpTransport {
101102
boolean contentLengthNotSpecified;
102103
boolean assertTestHeaders;
103104
boolean testIOException;
105+
boolean testMethodOverride;
104106
int maxByteIndexUploadedOnError = MediaHttpUploader.DEFAULT_CHUNK_SIZE - 1;
105107

106108
/**
@@ -121,7 +123,14 @@ static class MediaTransport extends MockHttpTransport {
121123
}
122124

123125
@Override
124-
public LowLevelHttpRequest buildRequest(String name, String url) {
126+
public boolean supportsMethod(String method) throws IOException {
127+
return method.equals(HttpMethods.POST)
128+
|| method.equals(HttpMethods.PUT)
129+
|| method.equals(HttpMethods.GET);
130+
}
131+
132+
@Override
133+
public LowLevelHttpRequest buildRequest(final String name, String url) {
125134
if (name.equals("POST")) {
126135
if (directUploadEnabled) {
127136
if (directUploadWithMetadata) {
@@ -148,6 +157,9 @@ public LowLevelHttpResponse execute() {
148157
if (assertTestHeaders) {
149158
assertEquals("test-header-value", getFirstHeaderValue("test-header-name"));
150159
}
160+
if (testMethodOverride) {
161+
assertEquals("PATCH", getFirstHeaderValue("X-HTTP-Method-Override"));
162+
}
151163
// This is the initiation call.
152164
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
153165
assertFalse(directUploadEnabled && testIOException);
@@ -408,6 +420,21 @@ public void testUploadOneCall() throws Exception {
408420
assertEquals(2, fakeTransport.lowLevelExecCalls);
409421
}
410422

423+
public void testUploadOneCall_WithPatch() throws Exception {
424+
int contentLength = MediaHttpUploader.DEFAULT_CHUNK_SIZE;
425+
MediaTransport fakeTransport = new MediaTransport(contentLength);
426+
fakeTransport.testMethodOverride = true;
427+
InputStream is = new ByteArrayInputStream(new byte[contentLength]);
428+
InputStreamContent mediaContent = new InputStreamContent(TEST_CONTENT_TYPE, is);
429+
mediaContent.setLength(contentLength);
430+
MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, fakeTransport, null);
431+
uploader.setInitiationRequestMethod(HttpMethods.PATCH);
432+
uploader.upload(new GenericUrl(TEST_RESUMABLE_REQUEST_URL));
433+
434+
// There should be 2 calls made. 1 initiation request and 1 upload request.
435+
assertEquals(2, fakeTransport.lowLevelExecCalls);
436+
}
437+
411438
public void testUploadOneCall_WithGZipDisabled() throws Exception {
412439
int contentLength = MediaHttpUploader.DEFAULT_CHUNK_SIZE;
413440
MediaTransport fakeTransport = new MediaTransport(contentLength);
@@ -495,6 +522,21 @@ public void testUploadMultipleCalls() throws Exception {
495522
assertEquals(6, fakeTransport.lowLevelExecCalls);
496523
}
497524

525+
public void testUploadMultipleCalls_WithPatch() throws Exception {
526+
int contentLength = MediaHttpUploader.DEFAULT_CHUNK_SIZE * 5;
527+
MediaTransport fakeTransport = new MediaTransport(contentLength);
528+
fakeTransport.testMethodOverride = true;
529+
InputStream is = new ByteArrayInputStream(new byte[contentLength]);
530+
InputStreamContent mediaContent = new InputStreamContent(TEST_CONTENT_TYPE, is);
531+
mediaContent.setLength(contentLength);
532+
MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, fakeTransport, null);
533+
uploader.setInitiationRequestMethod(HttpMethods.PATCH);
534+
uploader.upload(new GenericUrl(TEST_RESUMABLE_REQUEST_URL));
535+
536+
// There should be 6 calls made. 1 initiation request and 5 upload requests.
537+
assertEquals(6, fakeTransport.lowLevelExecCalls);
538+
}
539+
498540
public void testUploadMultipleCalls_WithSpecifiedHeader() throws Exception {
499541
int contentLength = MediaHttpUploader.DEFAULT_CHUNK_SIZE * 5;
500542
MediaTransport fakeTransport = new MediaTransport(contentLength);

0 commit comments

Comments
 (0)