com.github.jjYBdx4IL.test.URLTester Maven / Gradle / Ivy
/*
* Copyright (C) 2016 jjYBdx4IL (https://github.com/jjYBdx4IL)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.jjYBdx4IL.test;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Simple URL testing methods.
*
* Think about using {@link org.eclipse.jetty.http.HttpTester} instead.
*
* @author jjYBdx4IL
*/
public class URLTester {
public static void assertExists(String urlString) throws IOException {
URL url = new URL(urlString);
try (InputStream is = url.openStream()) {
} catch (FileNotFoundException ex) {
fail();
}
}
public static void assertNotFound(String urlString) throws IOException {
URL url = new URL(urlString);
Map> fields = url.openConnection().getHeaderFields();
String httpStatusLine = fields.get(null).get(0);
assertTrue(httpStatusLine, httpStatusLine.toLowerCase().matches("^\\S+\\s+404\\s+.*$"));
}
public static void assertNotExists(String urlString) throws IOException {
URL url = new URL(urlString);
// Map> fields = url.openConnection().getHeaderFields();
// for(String key : fields.keySet()) {
// for(String value : fields.get(key)) {
// log.info(key + " => " + value);
// }
// }
try (InputStream is = url.openStream()) {
fail();
} catch (FileNotFoundException ex) {
}
}
public static String getContent(String urlString) throws IOException {
URL url = new URL(urlString);
return IOUtils.toString(url.openStream());
}
/**
* Requests and returns partial http content starting at a specific position.
*
* Untested!
*
* Reads until the end of the stream.
*
*
* @param urlString
* @param firstBytePosition
* @return partial http content starting at a specific position
* @throws IOException
*/
public static String getContent(String urlString, long firstBytePosition) throws IOException {
return getContent(urlString, firstBytePosition, -1L);
}
/**
* Requests and returns partial http content starting and ending at specific positions.
*
* Untested!
*
* Behaves like {@link #getContent(String,long)} if
* lastBytePosition <= firstBytePosition
.
*
*
* @param urlString
* @param firstBytePosition starting at 0
* @param lastBytePosition starting at 0
* @return partial http content starting and ending at specific positions
* @throws IOException
*/
public static String getContent(String urlString, long firstBytePosition, long lastBytePosition)
throws IOException {
final String rangeParamValue;
if (lastBytePosition <= firstBytePosition) {
rangeParamValue = String.format("bytes=%1$d-", firstBytePosition);
} else {
rangeParamValue = String.format("bytes=%1$d-%2$d", firstBytePosition, lastBytePosition);
}
URL u = new URL(urlString);
URLConnection c = u.openConnection();
c.setRequestProperty("Range", rangeParamValue);
return IOUtils.toString(c.getInputStream());
}
/**
* Returns all header fields values.
*
* @param urlString
* @return
* @throws IOException
*/
@SuppressWarnings("unused")
public static Map> getHeaderFields(String urlString) throws IOException {
URL u = new URL(urlString);
URLConnection c = u.openConnection();
String content = IOUtils.toString(c.getInputStream());
return c.getHeaderFields();
}
public static void assertContains(String urlString, String needle) throws IOException {
assertTrue(getContent(urlString).indexOf(needle) > -1);
}
}