Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for CookieUtil and RequestAuthenticator helpers #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/test/java/org/jsoup/helper/CookieUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.jsoup.helper;

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.*;

import static org.junit.jupiter.api.Assertions.*;

/**
* This class tests the functionality of the CookieUtil class.
* It includes tests for applying cookies to a request, converting URLs to URIs, and storing cookies.
*/
public class CookieUtilTest {

@Test
public void testApplyCookiesToRequest() throws IOException {
HttpConnection.Request req = new HttpConnection.Request();
HttpURLConnection con = (HttpURLConnection) new URL("http://test.com").openConnection();
req.cookie("name", "value");

CookieUtil.applyCookiesToRequest(req, con);

assertEquals("name=value", con.getRequestProperty("Cookie"));
}

@Test
public void testAsUri() throws IOException, URISyntaxException {
URL url = new URL("http://test.com");
assertEquals(url.toURI(), CookieUtil.asUri(url));
}

@Test
public void testStoreCookies() throws IOException {
HttpConnection.Request req = new HttpConnection.Request();
URL url = new URL("http://test.com");
Map<String, List<String>> resHeaders = new HashMap<>();
resHeaders.put("Set-Cookie", Collections.singletonList("name=value"));

CookieUtil.storeCookies(req, url, resHeaders);

assertEquals("value", req.cookie("name"));
}
}
42 changes: 42 additions & 0 deletions src/test/java/org/jsoup/helper/RequestAuthenticatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.jsoup.helper;

import org.junit.jupiter.api.Test;

import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;

import static org.junit.jupiter.api.Assertions.*;

/**
* Test class for RequestAuthenticator.
* This class contains unit tests to verify the functionality of the RequestAuthenticator interface and its Context class.
*/
public class RequestAuthenticatorTest {

@Test public void testAuthenticate() throws Exception {
RequestAuthenticator auth = new RequestAuthenticator() {
@Override
public PasswordAuthentication authenticate(Context auth) {
if (auth.isServer() && "Realm".equals(auth.realm())) {
return auth.credentials("user", "pass");
}
return null;
}
};

RequestAuthenticator.Context context = new RequestAuthenticator.Context(
new URL("http://test.com"), Authenticator.RequestorType.SERVER, "Realm");

PasswordAuthentication credentials = auth.authenticate(context);
assertNotNull(credentials);
assertEquals("user", credentials.getUserName());
assertEquals("pass", new String(credentials.getPassword()));

RequestAuthenticator.Context wrongContext = new RequestAuthenticator.Context(
new URL("http://test.com"), Authenticator.RequestorType.PROXY, "WrongRealm");

PasswordAuthentication wrongCredentials = auth.authenticate(wrongContext);
assertNull(wrongCredentials);
}
}