<commit_before>package org.dmfs.oauth2.client.http.responsehandlers;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;

import org.dmfs.httpessentials.HttpStatus;
import org.dmfs.httpessentials.exceptions.ProtocolError;
import org.dmfs.httpessentials.exceptions.ProtocolException;
import org.dmfs.httpessentials.headers.EmptyHeaders;
import org.dmfs.httpessentials.mockutils.entities.StaticMockResponseEntity;
import org.dmfs.httpessentials.mockutils.responses.StaticMockResponse;
import org.dmfs.httpessentials.types.StructuredMediaType;
import org.dmfs.oauth2.client.errors.TokenRequestError;
import org.junit.Test;


/**
 * Test the {@link TokenErrorResponseHandler}.
 * <p />
 * TODO: also test some invalid responses.
 * 
 * @author <NAME> <<EMAIL>>
 */
public class TokenErrorResponseHandlerTest
{

	@Test
	public void testHandleResponse() throws UnsupportedEncodingException, IOException, ProtocolError, ProtocolException
	{
		final String accessTokenResponse = "{" + "\"error\":\"invalid_grant\"," + "\"error_description\":\"DESCRIPTION\","
			+ "\"error_uri\":\"http://example.com\"" + "}";

		try
		{
			new TokenErrorResponseHandler().handleResponse(new StaticMockResponse(HttpStatus.BAD_REQUEST, EmptyHeaders.INSTANCE, new StaticMockResponseEntity(
				new StructuredMediaType("application", "json", "utf-8"), accessTokenResponse)));
			fail("No exception thrown");
		}
		catch (TokenRequestError e)
		{
			assertEquals("invalid_grant", e.getMessage());
			assertEquals("DESCRIPTION", e.description());
			assertEquals(URI.create("http://example.com"), e.uri());
		}
		catch (Exception t)
		{
			fail(String.format("wrong exception type thrown: %s", t.getClass().getName()));
		}
	}
}<commit_msg>improve TokenErrorResponseHandlerTest<commit_after>package org.dmfs.oauth2.client.http.responsehandlers;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;

import org.dmfs.httpessentials.HttpStatus;
import org.dmfs.httpessentials.exceptions.ProtocolError;
import org.dmfs.httpessentials.exceptions.ProtocolException;
import org.dmfs.httpessentials.headers.EmptyHeaders;
import org.dmfs.httpessentials.mockutils.entities.StaticMockResponseEntity;
import org.dmfs.httpessentials.mockutils.responses.StaticMockResponse;
import org.dmfs.httpessentials.types.StructuredMediaType;
import org.dmfs.oauth2.client.errors.TokenRequestError;
import org.junit.Test;


/**
 * Test the {@link TokenErrorResponseHandler}.
 * <p />
 * TODO: also test some invalid responses.
 * 
 * @author <NAME> <<EMAIL>>
 */
public class TokenErrorResponseHandlerTest
{

	@Test
	public void testHandleResponseNoUri() throws UnsupportedEncodingException, IOException, ProtocolError, ProtocolException
	{
		final String accessTokenResponse = "{" + "\"error\":\"invalid_grant\"," + "\"error_description\":\"DESCRIPTION\"," + "}";

		try
		{
			new TokenErrorResponseHandler().handleResponse(new StaticMockResponse(HttpStatus.BAD_REQUEST, EmptyHeaders.INSTANCE, new StaticMockResponseEntity(
				new StructuredMediaType("application", "json", "utf-8"), accessTokenResponse)));
			fail("No exception thrown");
		}
		catch (TokenRequestError e)
		{
			assertEquals("invalid_grant", e.getMessage());
			assertEquals("DESCRIPTION", e.description());
			assertNull(e.uri());
		}
		catch (Exception t)
		{
			fail(String.format("wrong exception type thrown: %s", t.getClass().getName()));
		}
	}


	@Test
	public void testHandleResponseNoDescription() throws UnsupportedEncodingException, IOException, ProtocolError, ProtocolException
	{
		final String accessTokenResponse = "{" + "\"error\":\"invalid_grant\"," + "\"error_uri\":\"http://example.com\"" + "}";

		try
		{
			new TokenErrorResponseHandler().handleResponse(new StaticMockResponse(HttpStatus.BAD_REQUEST, EmptyHeaders.INSTANCE, new StaticMockResponseEntity(
				new StructuredMediaType("application", "json", "utf-8"), accessTokenResponse)));
			fail("No exception thrown");
		}
		catch (TokenRequestError e)
		{
			assertEquals("invalid_grant", e.getMessage());
			assertEquals("", e.description());
			assertEquals(URI.create("http://example.com"), e.uri());
		}
		catch (Exception t)
		{
			fail(String.format("wrong exception type thrown: %s", t.getClass().getName()));
		}
	}


	@Test
	public void testHandleResponse() throws UnsupportedEncodingException, IOException, ProtocolError, ProtocolException
	{
		final String accessTokenResponse = "{" + "\"error\":\"invalid_grant\"," + "\"error_description\":\"DESCRIPTION\","
			+ "\"error_uri\":\"http://example.com\"" + "}";

		try
		{
			new TokenErrorResponseHandler().handleResponse(new StaticMockResponse(HttpStatus.BAD_REQUEST, EmptyHeaders.INSTANCE, new StaticMockResponseEntity(
				new StructuredMediaType("application", "json", "utf-8"), accessTokenResponse)));
			fail("No exception thrown");
		}
		catch (TokenRequestError e)
		{
			assertEquals("invalid_grant", e.getMessage());
			assertEquals("DESCRIPTION", e.description());
			assertEquals(URI.create("http://example.com"), e.uri());
		}
		catch (Exception t)
		{
			fail(String.format("wrong exception type thrown: %s", t.getClass().getName()));
		}
	}
}
{'fraction_non_alphanumeric': 0.1419319022945966, 'fraction_numerical': 0.0014803849000740192, 'mean_word_length': 5.656403940886699, 'pattern_counts': {'":': 10, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4559345', 'n_tokens_mistral': 1690, 'n_tokens_neox': 1560, 'n_words': 291}