Skip to content

Instantly share code, notes, and snippets.

Created January 2, 2013 17:40
Show Gist options
  • Save anonymous/4436395 to your computer and use it in GitHub Desktop.
Save anonymous/4436395 to your computer and use it in GitHub Desktop.
Test percent encoding of path segment values in Spring HATEOAS
package com.tmobile.tmo.cms.web.api.document.assembler;
import static org.junit.Assert.assertEquals;
import static org.springframework.hateoas.mvc.BasicLinkBuilder.linkToCurrentMapping;
import org.junit.Before;
import org.junit.Test;
import org.springframework.hateoas.Link;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
public class SpringHateoasLinkTest {
@Before
public void setup() {
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(new MockHttpServletRequest()));
}
@Test
public void specialCharactersInPathShouldNotBeDoubleEncoded() {
Link l = linkToCurrentMapping().slash("first").slash("sec%ond").slash("thi%rd").slash("fourth").withSelfRel();
assertEquals("http://localhost/first/sec%25ond/thi%25rd/fourth", l.getHref());
}
@Test
public void slashInPathPathShouldBeEncoded() {
Link l = linkToCurrentMapping().slash("first").slash("sec/ond").slash("third").withSelfRel();
assertEquals("http://localhost/first/sec%2Fond/third/fourth", l.getHref());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment