Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: redirect on 308 (Permanent Redirect) too #876

Merged
merged 2 commits into from Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -57,6 +57,9 @@ public class HttpStatusCodes {
/** Status code for a resource that has temporarily moved to a new URI. */
public static final int STATUS_CODE_TEMPORARY_REDIRECT = 307;

/** Status code for a resource that has permanently moved to a new URI. */
private static final int STATUS_CODE_PERMANENT_REDIRECT = 308;

/** Status code for a request that could not be understood by the server. */
public static final int STATUS_CODE_BAD_REQUEST = 400;

Expand Down Expand Up @@ -109,7 +112,7 @@ public static boolean isSuccess(int statusCode) {

/**
* Returns whether the given HTTP response status code is a redirect code {@code 301, 302, 303,
* 307}.
* 307, 308}.
*
* @since 1.11
*/
Expand All @@ -119,6 +122,7 @@ public static boolean isRedirect(int statusCode) {
case HttpStatusCodes.STATUS_CODE_FOUND: // 302
case HttpStatusCodes.STATUS_CODE_SEE_OTHER: // 303
case HttpStatusCodes.STATUS_CODE_TEMPORARY_REDIRECT: // 307
case HttpStatusCodes.STATUS_CODE_PERMANENT_REDIRECT: // 308
return true;
default:
return false;
Expand Down
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2019 Google Inc.
chanseokoh marked this conversation as resolved.
Show resolved Hide resolved
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.api.client.http;

import junit.framework.TestCase;

/** Tests {@link HttpStatusCodes}. */
public class HttpStatusCodesTest extends TestCase {

public void testIsRedirect_3xx() {
assertTrue(HttpStatusCodes.isRedirect(301));
assertTrue(HttpStatusCodes.isRedirect(302));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about 309 to 399? These are all undefined redirect codes according to HTTP.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the method name should really be isDefinedRedirect or such, given where this code is used to handle automatic redirection. The Javadoc at least describes the behavior correctly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The behavior for other 3xx codes is not defined in docs or tests. I suspect the current behavior in this case never got much thought.

Think of it this way: if we do see a 315 code what should we do with it? The answer is treat it as a redirect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't disagree. The library could be modified to redirect on all 3xx, but I don't feel like I will introduce that kind of behavior change myself right now. That sounds like we need more discussions, and I don't feel confident to introduce such a change, as I don't think I have enough expertise on this matter. For now, I will just allow redirecting 308 that at least should have been covered here. Nothing more, just one change that I believe should be fixed at least.

assertTrue(HttpStatusCodes.isRedirect(303));
assertTrue(HttpStatusCodes.isRedirect(307));
assertTrue(HttpStatusCodes.isRedirect(308));
}

public void testIsRedirect_non3xx() {
assertFalse(HttpStatusCodes.isRedirect(200));
assertFalse(HttpStatusCodes.isRedirect(401));
assertFalse(HttpStatusCodes.isRedirect(500));
}
}