Set content type for RDAP responses before setting the payload

Also sets the charset to UTF8 explicitly, to avoid confusion, and removes apparently unneeded Truth8 include.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=171963981
This commit is contained in:
mountford 2017-10-12 08:42:52 -07:00 committed by jianglai
parent c0f8da0c6e
commit 9d1eb0d429
2 changed files with 12 additions and 4 deletions

View file

@ -14,6 +14,7 @@
package google.registry.rdap; package google.registry.rdap;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Preconditions.checkState;
import static com.google.common.net.HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN; import static com.google.common.net.HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN;
@ -76,7 +77,8 @@ public abstract class RdapActionBase implements Runnable {
*/ */
static final Pattern LDH_PATTERN = Pattern.compile("[-.a-zA-Z0-9*]+"); static final Pattern LDH_PATTERN = Pattern.compile("[-.a-zA-Z0-9*]+");
private static final MediaType RESPONSE_MEDIA_TYPE = MediaType.create("application", "rdap+json"); private static final MediaType RESPONSE_MEDIA_TYPE =
MediaType.create("application", "rdap+json").withCharset(UTF_8);
@Inject HttpServletRequest request; @Inject HttpServletRequest request;
@Inject Response response; @Inject Response response;
@ -131,10 +133,10 @@ public abstract class RdapActionBase implements Runnable {
requestMethod == Action.Method.HEAD, requestMethod == Action.Method.HEAD,
rdapLinkBase); rdapLinkBase);
response.setStatus(SC_OK); response.setStatus(SC_OK);
response.setContentType(RESPONSE_MEDIA_TYPE);
if (requestMethod != Action.Method.HEAD) { if (requestMethod != Action.Method.HEAD) {
response.setPayload(JSONValue.toJSONString(rdapJson)); response.setPayload(JSONValue.toJSONString(rdapJson));
} }
response.setContentType(RESPONSE_MEDIA_TYPE);
} catch (HttpException e) { } catch (HttpException e) {
setError(e.getResponseCode(), e.getResponseCodeString(), e.getMessage()); setError(e.getResponseCode(), e.getResponseCodeString(), e.getMessage());
} catch (URISyntaxException | IllegalArgumentException e) { } catch (URISyntaxException | IllegalArgumentException e) {
@ -147,12 +149,12 @@ public abstract class RdapActionBase implements Runnable {
void setError(int status, String title, String description) { void setError(int status, String title, String description) {
response.setStatus(status); response.setStatus(status);
response.setContentType(RESPONSE_MEDIA_TYPE);
try { try {
if (requestMethod != Action.Method.HEAD) { if (requestMethod != Action.Method.HEAD) {
response.setPayload( response.setPayload(
JSONValue.toJSONString(rdapJsonFormatter.makeError(status, title, description))); JSONValue.toJSONString(rdapJsonFormatter.makeError(status, title, description)));
} }
response.setContentType(RESPONSE_MEDIA_TYPE);
} catch (Exception ex) { } catch (Exception ex) {
if (requestMethod != Action.Method.HEAD) { if (requestMethod != Action.Method.HEAD) {
response.setPayload(""); response.setPayload("");

View file

@ -16,7 +16,6 @@ package google.registry.rdap;
import static com.google.common.net.HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN; import static com.google.common.net.HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;
import static google.registry.request.Action.Method.GET; import static google.registry.request.Action.Method.GET;
import static google.registry.request.Action.Method.HEAD; import static google.registry.request.Action.Method.HEAD;
import static google.registry.testing.DatastoreHelper.createTld; import static google.registry.testing.DatastoreHelper.createTld;
@ -143,6 +142,13 @@ public class RdapActionBaseTest {
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
} }
@Test
public void testContentType_rdapjson_utf8() throws Exception {
generateActualJson("no.thing");
assertThat(response.getContentType().toString())
.isEqualTo("application/rdap+json; charset=utf-8");
}
@Test @Test
public void testHeadRequest_returnsNoContent() throws Exception { public void testHeadRequest_returnsNoContent() throws Exception {
assertThat(generateHeadPayload("no.thing")).isEmpty(); assertThat(generateHeadPayload("no.thing")).isEmpty();