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

RestTemplate does not render correct url for server ports [SPR-12123] #16739

Closed
spring-projects-issues opened this issue Aug 26, 2014 · 2 comments
Closed
Assignees
Labels
in: web type: enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Aug 26, 2014

Mattias Severson opened SPR-12123 and commented

When using a uri template such as

http://localhost:{port}/resource together with RestTemplate, the ":" separating the host from the port gets rendered as a "/".


Example:

public class RestTemplateTest {

    RestTemplate restTemplate;
    MockRestServiceServer mockServer;

    @Before
    public void setUp() {
        restTemplate = new RestTemplate();
        mockServer = MockRestServiceServer.createServer(restTemplate);
    }

    @Test
    public void testRestTemplate() {
        mockServer.expect(
                requestTo("http://localhost:8080/resource")).
                andExpect(method(HttpMethod.GET)).
                andRespond(withSuccess("Success", MediaType.TEXT_PLAIN));

        String response = restTemplate.getForObject(
            "http://localhost:{port}/resource", 
            String.class, 
            8080);
        assertThat(response, equalTo("Success"));
    }
}

Expected result:
the test should pass

Actual result:

java.lang.AssertionError: Request URI 
Expected :http://localhost:8080/resource
Actual   :http://localhost/8080/resource

This causes problems when implementing integration tests using Spring Boot's local.server.port, e.g.

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest("server.port:0")
public class SomeIntegrationTest {
    @Value("${local.server.port}")
    int serverPort;

    @Test
    public void getsSomeResource() throws Exception {
        String response = restTemplate.getForObject(
            "http://localhost:{port}/resource", 
            String.class, 
            serverPort);
    }
}

Yet again, the ":" between the host and port is replaced by "/" and the test fails accordingly:

ResourceAccessException: I/O error on GET request for "http://localhost/57293/resource":Connection refused;

Related: this question at Stack Overflow.


Affects: 4.0.6

Referenced from: commits 8fbd310, a0b231d

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Aug 26, 2014

Rossen Stoyanchev commented

It looks like the port regex in UriComponentsBuilder only allows digits. Furthermore UriComponentsBuilder has an int port field, the constructor of HierarchicalUriComponents expects port to be an int and expandInternal does not do anything with the port.

That seems quite deliberate actually although I can't quite see what may be the reason. Arjen Poutsma can you think of any background that I may be missing?

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Aug 26, 2014

Mattias Severson commented

Added missing "not" to issue title.

@spring-projects-issues spring-projects-issues added type: enhancement in: web labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 4.1 GA milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web type: enhancement
Projects
None yet
Development

No branches or pull requests

2 participants