Update port related code in UriComponentsBuilder

Use null to indicate no port has been provided.
Raise exception when port has not been expanded.

Issue: SPR-12123
This commit is contained in:
Rossen Stoyanchev
2014-08-29 09:21:15 -04:00
parent 8fbd310b07
commit a0b231d36d
3 changed files with 35 additions and 27 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
package org.springframework.web.util;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
@@ -23,10 +25,12 @@ import java.io.ObjectOutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import org.junit.Test;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.springframework.web.util.UriComponentsBuilder.fromUriString;
/**
* @author Arjen Poutsma
@@ -84,22 +88,18 @@ public class UriComponentsTests {
@Test
public void port() {
UriComponents uriComponents1 = UriComponentsBuilder.fromUriString(
"http://example.com:8080/bar").build();
UriComponents uriComponents2 = UriComponentsBuilder.fromUriString(
"http://example.com/bar").port(8080).build();
UriComponents uriComponents3 = UriComponentsBuilder.fromUriString(
"http://example.com/bar").port("{port}").build().expand(8080);
UriComponents uriComponents4 = UriComponentsBuilder.fromUriString(
"http://example.com/bar").port("808{digit}").build().expand(0);
assertEquals(8080, uriComponents1.getPort());
assertEquals("http://example.com:8080/bar", uriComponents1.toUriString());
assertEquals(8080, uriComponents2.getPort());
assertEquals("http://example.com:8080/bar", uriComponents2.toUriString());
assertEquals(8080, uriComponents3.getPort());
assertEquals("http://example.com:8080/bar", uriComponents3.toUriString());
assertEquals(8080, uriComponents4.getPort());
assertEquals("http://example.com:8080/bar", uriComponents4.toUriString());
UriComponents uri1 = fromUriString("http://example.com:8080/bar").build();
UriComponents uri2 = fromUriString("http://example.com/bar").port(8080).build();
UriComponents uri3 = fromUriString("http://example.com/bar").port("{port}").build().expand(8080);
UriComponents uri4 = fromUriString("http://example.com/bar").port("808{digit}").build().expand(0);
assertEquals(8080, uri1.getPort());
assertEquals("http://example.com:8080/bar", uri1.toUriString());
assertEquals(8080, uri2.getPort());
assertEquals("http://example.com:8080/bar", uri2.toUriString());
assertEquals(8080, uri3.getPort());
assertEquals("http://example.com:8080/bar", uri3.toUriString());
assertEquals(8080, uri4.getPort());
assertEquals("http://example.com:8080/bar", uri4.toUriString());
}
@Test(expected = IllegalStateException.class)