Skip to content

Commit

Permalink
test and fix for issue #133
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Jan 18, 2017
1 parent d5f6ce9 commit 8917204
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,17 @@ public RESTConfluenceServiceImpl( String url , Credentials credentials, SSLCerti

private HttpUrl.Builder urlBuilder() {

int port = endpoint.getPort();
port = (port > -1 ) ? port : endpoint.getDefaultPort();

String path = endpoint.getPath();
path = (path.startsWith("/")) ? path.substring(1) : path;

return new HttpUrl.Builder()
.scheme(endpoint.getProtocol())
.host(endpoint.getHost())
.port(endpoint.getPort())
.port(port)
.addPathSegments(path)
.addPathSegments("rest/api")
;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.bsc.maven.plugin.test;

import java.net.MalformedURLException;
import java.net.URISyntaxException;
import okhttp3.HttpUrl;
import org.hamcrest.core.Is;
import org.hamcrest.core.IsEqual;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

/**
*
* @author softphone
*/
public class Issue133Test {

@Test @Ignore
public void dummy() {}


@Test(expected = java.lang.IllegalArgumentException.class)
public void htttpUrlBuilderWithoutPort() throws MalformedURLException {

final java.net.URL endpoint = new java.net.URL("http://localhost/confluence");

Assert.assertThat( endpoint.getPort(), Is.is(-1));

final HttpUrl.Builder builder = new HttpUrl.Builder()
.scheme(endpoint.getProtocol())
.host(endpoint.getHost())
.port(endpoint.getPort())
.addPathSegments("rest/api")
;

}

@Test
public void htttpUrlBuilderWithoutPortSafe() throws MalformedURLException {

final java.net.URL endpoint = new java.net.URL("http://localhost/confluence");

Assert.assertThat( endpoint.getPort(), Is.is(-1));

int port = endpoint.getPort();
port = (port > -1 ) ? port : endpoint.getDefaultPort();

String path = endpoint.getPath();
path = (path.startsWith("/")) ? path.substring(1) : path;

final HttpUrl.Builder builder = new HttpUrl.Builder()
.scheme(endpoint.getProtocol())
.host(endpoint.getHost())
.port( port )
;
Assert.assertThat(path, IsEqual.equalTo("confluence"));

final HttpUrl url =
builder
.addPathSegments(path)
//.addPathSegments("rest/api")
.build();

Assert.assertThat( url.url(), IsEqual.equalTo(endpoint));

}

@Test
public void htttpUrlBuilderWithoutPortSafe2() throws MalformedURLException, URISyntaxException {

final java.net.URL endpoint = new java.net.URL("http://localhost/");

Assert.assertThat( endpoint.getPort(), Is.is(-1));

int port = endpoint.getPort();
port = (port > -1 ) ? port : endpoint.getDefaultPort();

String path = endpoint.getPath();
path = (path.startsWith("/")) ? path.substring(1) : path;

final HttpUrl.Builder builder = new HttpUrl.Builder()
.scheme(endpoint.getProtocol())
.host(endpoint.getHost())
.port( port )
;

Assert.assertThat( path, IsEqual.equalTo(""));

final HttpUrl url =
builder
.addPathSegments(path)
//.addPathSegments("rest/api")
.build();

Assert.assertThat( url.url(), IsEqual.equalTo(endpoint));
;

}
}

0 comments on commit 8917204

Please sign in to comment.