|
| 1 | +/** |
| 2 | + * Copyright (C) 2015-2022 Red Hat, Inc. (http://github.com/Commonjava/http-testserver) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.commonjava.test.http.expect; |
| 17 | + |
| 18 | +import org.apache.commons.io.IOUtils; |
| 19 | +import org.apache.http.client.methods.CloseableHttpResponse; |
| 20 | +import org.apache.http.client.methods.HttpGet; |
| 21 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 22 | +import org.apache.http.impl.client.HttpClients; |
| 23 | +import org.commonjava.test.http.common.CommonMethod; |
| 24 | +import org.junit.Rule; |
| 25 | +import org.junit.Test; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | +import java.io.InputStream; |
| 29 | + |
| 30 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 31 | +import static org.junit.Assert.assertThat; |
| 32 | + |
| 33 | +public class ExpectationServerTest |
| 34 | +{ |
| 35 | + |
| 36 | + @Rule |
| 37 | + public ExpectationServer server = new ExpectationServer( "repos" ); |
| 38 | + |
| 39 | + private final String subPath = "/path/to/something"; |
| 40 | + |
| 41 | + @Test |
| 42 | + public void downloadWithQueryParams() |
| 43 | + throws Exception |
| 44 | + { |
| 45 | + final String path1 = subPath + "?version=1.0"; |
| 46 | + final String path2 = subPath + "?version=2.0"; |
| 47 | + |
| 48 | + final String url1 = server.formatUrl(path1); |
| 49 | + final String url2 = server.formatUrl(path2); |
| 50 | + |
| 51 | + final String content1 = "this is a test version 1"; |
| 52 | + final String content2 = "this is a test version 2"; |
| 53 | + |
| 54 | + server.expect(url1, 200, content1); |
| 55 | + server.expect(url2, 200, content2); |
| 56 | + |
| 57 | + String result = getHttpContent(url1); |
| 58 | + assertThat(result, equalTo(content1)); |
| 59 | + |
| 60 | + result = getHttpContent(url2); |
| 61 | + assertThat(result, equalTo(content2)); |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + private String getHttpContent(String url) throws IOException |
| 66 | + { |
| 67 | + final HttpGet request = new HttpGet( url ); |
| 68 | + final CloseableHttpClient client = HttpClients.createDefault(); |
| 69 | + |
| 70 | + try(CloseableHttpResponse response = client.execute( request )) |
| 71 | + { |
| 72 | + InputStream stream = response.getEntity().getContent(); |
| 73 | + return IOUtils.toString( stream ); |
| 74 | + } |
| 75 | + finally |
| 76 | + { |
| 77 | + IOUtils.closeQuietly( client ); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments