2023-03-04 12:19:01 +00:00
|
|
|
package nu.marginalia.crawling;
|
2022-05-19 15:45:26 +00:00
|
|
|
|
|
|
|
import lombok.SneakyThrows;
|
2023-03-04 12:19:01 +00:00
|
|
|
import nu.marginalia.crawl.retreival.RateLimitException;
|
2023-07-21 17:47:52 +00:00
|
|
|
import nu.marginalia.crawl.retreival.fetcher.ContentTags;
|
2023-06-24 18:09:54 +00:00
|
|
|
import nu.marginalia.crawl.retreival.fetcher.HttpFetcherImpl;
|
2023-12-13 14:33:42 +00:00
|
|
|
import nu.marginalia.crawling.body.DocumentBodyExtractor;
|
|
|
|
import nu.marginalia.crawling.body.DocumentBodyResult;
|
2023-12-08 16:12:51 +00:00
|
|
|
import nu.marginalia.crawl.retreival.fetcher.warc.WarcRecorder;
|
2023-12-13 14:33:42 +00:00
|
|
|
import nu.marginalia.crawling.body.ContentTypeLogic;
|
2023-03-04 12:19:01 +00:00
|
|
|
import nu.marginalia.model.EdgeUrl;
|
2022-05-19 15:45:26 +00:00
|
|
|
import org.junit.jupiter.api.Assertions;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
2023-12-08 16:12:51 +00:00
|
|
|
import java.io.IOException;
|
2022-05-19 15:45:26 +00:00
|
|
|
import java.net.URISyntaxException;
|
|
|
|
|
|
|
|
class HttpFetcherTest {
|
|
|
|
|
|
|
|
@SneakyThrows
|
|
|
|
@Test
|
|
|
|
void testUrlPattern() {
|
2022-08-18 16:25:09 +00:00
|
|
|
ContentTypeLogic contentTypeLogic = new ContentTypeLogic();
|
|
|
|
|
|
|
|
Assertions.assertFalse(contentTypeLogic.isUrlLikeBinary(new EdgeUrl("https://marginalia.nu/log.txt")));
|
|
|
|
Assertions.assertTrue(contentTypeLogic.isUrlLikeBinary(new EdgeUrl("https://marginalia.nu/log.bin")));
|
|
|
|
Assertions.assertTrue(contentTypeLogic.isUrlLikeBinary(new EdgeUrl("https://marginalia.nu/log.tar.gz")));
|
|
|
|
Assertions.assertFalse(contentTypeLogic.isUrlLikeBinary(new EdgeUrl("https://marginalia.nu/log.htm")));
|
|
|
|
Assertions.assertFalse(contentTypeLogic.isUrlLikeBinary(new EdgeUrl("https://marginalia.nu/log.html")));
|
|
|
|
Assertions.assertFalse(contentTypeLogic.isUrlLikeBinary(new EdgeUrl("https://marginalia.nu/log")));
|
|
|
|
Assertions.assertFalse(contentTypeLogic.isUrlLikeBinary(new EdgeUrl("https://marginalia.nu/log.php?id=1")));
|
2022-05-19 15:45:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2023-12-08 16:12:51 +00:00
|
|
|
void fetchUTF8() throws URISyntaxException, RateLimitException, IOException {
|
2023-06-24 18:09:54 +00:00
|
|
|
var fetcher = new HttpFetcherImpl("nu.marginalia.edge-crawler");
|
2023-12-08 16:12:51 +00:00
|
|
|
try (var recorder = new WarcRecorder()) {
|
2023-12-13 14:33:42 +00:00
|
|
|
var result = fetcher.fetchContent(new EdgeUrl("https://www.marginalia.nu"), recorder, ContentTags.empty());
|
2023-12-14 15:05:48 +00:00
|
|
|
if (DocumentBodyExtractor.asString(result) instanceof DocumentBodyResult.Ok bodyOk) {
|
2023-12-13 14:33:42 +00:00
|
|
|
System.out.println(bodyOk.contentType());
|
|
|
|
}
|
2023-12-08 16:12:51 +00:00
|
|
|
}
|
2022-05-19 15:45:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2023-12-08 16:12:51 +00:00
|
|
|
void fetchText() throws URISyntaxException, RateLimitException, IOException {
|
2023-06-24 18:09:54 +00:00
|
|
|
var fetcher = new HttpFetcherImpl("nu.marginalia.edge-crawler");
|
2023-12-08 16:12:51 +00:00
|
|
|
|
|
|
|
try (var recorder = new WarcRecorder()) {
|
2023-12-13 14:33:42 +00:00
|
|
|
var result = fetcher.fetchContent(new EdgeUrl("https://www.marginalia.nu/robots.txt"), recorder, ContentTags.empty());
|
2023-12-14 15:05:48 +00:00
|
|
|
if (DocumentBodyExtractor.asString(result) instanceof DocumentBodyResult.Ok bodyOk) {
|
2023-12-13 14:33:42 +00:00
|
|
|
System.out.println(bodyOk.contentType());
|
|
|
|
}
|
2023-12-08 16:12:51 +00:00
|
|
|
}
|
2022-05-19 15:45:26 +00:00
|
|
|
}
|
|
|
|
}
|