|
| 1 | +package com.xatkit.core.server; |
| 2 | + |
| 3 | +import com.xatkit.AbstractXatkitTest; |
| 4 | +import org.apache.http.HttpRequest; |
| 5 | +import org.apache.http.NameValuePair; |
| 6 | +import org.apache.http.client.utils.URIBuilder; |
| 7 | +import org.apache.http.message.BasicHttpRequest; |
| 8 | +import org.apache.http.message.BasicNameValuePair; |
| 9 | +import org.junit.BeforeClass; |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +import java.net.URISyntaxException; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +import static org.assertj.core.api.Assertions.assertThat; |
| 17 | + |
| 18 | +public class HttpUtilsTest extends AbstractXatkitTest { |
| 19 | + |
| 20 | + private static String VALID_HTTP_REQUEST_PATH = "/target"; |
| 21 | + |
| 22 | + private static HttpRequest VALID_HTTP_REQUEST; |
| 23 | + |
| 24 | + private static HttpRequest INVALID_HTTP_REQUEST; |
| 25 | + |
| 26 | + private static List<NameValuePair> TEST_PARAMETERS = Arrays.asList(new BasicNameValuePair("param1", "abc"), |
| 27 | + new BasicNameValuePair("param2", "def")); |
| 28 | + |
| 29 | + @BeforeClass |
| 30 | + public static void setUpBeforeClass() { |
| 31 | + VALID_HTTP_REQUEST = new BasicHttpRequest("GET", VALID_HTTP_REQUEST_PATH); |
| 32 | + INVALID_HTTP_REQUEST = new BasicHttpRequest("GET", "i\nvalid"); |
| 33 | + } |
| 34 | + |
| 35 | + @Test(expected = NullPointerException.class) |
| 36 | + public void getURIBuilderFromNullRequest() throws URISyntaxException { |
| 37 | + HttpUtils.getURIBuilderFrom(null); |
| 38 | + } |
| 39 | + |
| 40 | + @Test(expected = URISyntaxException.class) |
| 41 | + public void getURIBuilderFromInvalidURI() throws URISyntaxException { |
| 42 | + HttpUtils.getURIBuilderFrom(INVALID_HTTP_REQUEST); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void getURIBuilderFromValidURI() throws URISyntaxException { |
| 47 | + URIBuilder uriBuilder = HttpUtils.getURIBuilderFrom(VALID_HTTP_REQUEST); |
| 48 | + assertThat(uriBuilder.getPath()).as("Builder contains the correct path").isEqualTo(VALID_HTTP_REQUEST_PATH); |
| 49 | + } |
| 50 | + |
| 51 | + @Test(expected = NullPointerException.class) |
| 52 | + public void getPathNullRequest() throws URISyntaxException { |
| 53 | + HttpUtils.getPath(null); |
| 54 | + } |
| 55 | + |
| 56 | + @Test(expected = URISyntaxException.class) |
| 57 | + public void getPathInvalidURI() throws URISyntaxException { |
| 58 | + HttpUtils.getPath(INVALID_HTTP_REQUEST); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void getPathValidURI() throws URISyntaxException { |
| 63 | + String path = HttpUtils.getPath(VALID_HTTP_REQUEST); |
| 64 | + assertThat(path).as("Correct path").isEqualTo(VALID_HTTP_REQUEST_PATH); |
| 65 | + } |
| 66 | + |
| 67 | + @Test(expected = NullPointerException.class) |
| 68 | + public void getParametersNullRequest() throws URISyntaxException { |
| 69 | + HttpUtils.getParameters(null); |
| 70 | + } |
| 71 | + |
| 72 | + @Test(expected = URISyntaxException.class) |
| 73 | + public void getParametersInvalidURI() throws URISyntaxException { |
| 74 | + HttpUtils.getParameters(INVALID_HTTP_REQUEST); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void getParametersValidURINoParameters() throws URISyntaxException { |
| 79 | + List<NameValuePair> parameters = HttpUtils.getParameters(VALID_HTTP_REQUEST); |
| 80 | + assertThat(parameters).as("Parameter list is empty").isEmpty(); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void getParametersValidURIWithParameters() throws URISyntaxException { |
| 85 | + List<NameValuePair> parameters = HttpUtils.getParameters(new BasicHttpRequest("GET", |
| 86 | + VALID_HTTP_REQUEST_PATH + "?param1=abc¶m2=def")); |
| 87 | + assertThat(parameters).as("Parameters list contains two parameters").hasSize(2); |
| 88 | + assertThat(parameters.stream()).as("Parameter list contains param1").anyMatch(p -> p.getName().equals("param1" |
| 89 | + ) && p.getValue().equals("abc")); |
| 90 | + assertThat(parameters.stream()).as("Parameter list contains param2").anyMatch(p -> p.getName().equals("param2" |
| 91 | + ) && p.getValue().equals("def")); |
| 92 | + } |
| 93 | + |
| 94 | + @Test(expected = NullPointerException.class) |
| 95 | + public void getParameterValueNullName() { |
| 96 | + HttpUtils.getParameterValue(null, TEST_PARAMETERS); |
| 97 | + } |
| 98 | + |
| 99 | + @Test(expected = NullPointerException.class) |
| 100 | + public void getParameterValueNullParameters() { |
| 101 | + HttpUtils.getParameterValue("param1", null); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void getParameterValueExistingParameter() { |
| 106 | + String value = HttpUtils.getParameterValue("param1", TEST_PARAMETERS); |
| 107 | + assertThat(value).as("Valid parameter value").isEqualTo("abc"); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void getParameterValueNotExistingParameter() { |
| 112 | + String value = HttpUtils.getParameterValue("invalid", TEST_PARAMETERS); |
| 113 | + assertThat(value).as("Parameter value is null").isNull(); |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments