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 Marten Gajda <marten@dmfs.org>
 */
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()));
		}
	}
}

@@ -93,16 +93,59 @@
 Equals;%0A
+import static org.junit.Assert.assertNull;%0A
 import s
@@ -968,16 +968,1681 @@
 Test%0A%7B%0A%0A
+%09@Test%0A%09public void testHandleResponseNoUri() throws UnsupportedEncodingException, IOException, ProtocolError, ProtocolException%0A%09%7B%0A%09%09final String accessTokenResponse = %22%7B%22 + %22%5C%22error%5C%22:%5C%22invalid_grant%5C%22,%22 + %22%5C%22error_description%5C%22:%5C%22DESCRIPTION%5C%22,%22 + %22%7D%22;%0A%0A%09%09try%0A%09%09%7B%0A%09%09%09new TokenErrorResponseHandler().handleResponse(new StaticMockResponse(HttpStatus.BAD_REQUEST, EmptyHeaders.INSTANCE, new StaticMockResponseEntity(%0A%09%09%09%09new StructuredMediaType(%22application%22, %22json%22, %22utf-8%22), accessTokenResponse)));%0A%09%09%09fail(%22No exception thrown%22);%0A%09%09%7D%0A%09%09catch (TokenRequestError e)%0A%09%09%7B%0A%09%09%09assertEquals(%22invalid_grant%22, e.getMessage());%0A%09%09%09assertEquals(%22DESCRIPTION%22, e.description());%0A%09%09%09assertNull(e.uri());%0A%09%09%7D%0A%09%09catch (Exception t)%0A%09%09%7B%0A%09%09%09fail(String.format(%22wrong exception type thrown: %25s%22, t.getClass().getName()));%0A%09%09%7D%0A%09%7D%0A%0A%0A%09@Test%0A%09public void testHandleResponseNoDescription() throws UnsupportedEncodingException, IOException, ProtocolError, ProtocolException%0A%09%7B%0A%09%09final String accessTokenResponse = %22%7B%22 + %22%5C%22error%5C%22:%5C%22invalid_grant%5C%22,%22 + %22%5C%22error_uri%5C%22:%5C%22http://example.com%5C%22%22 + %22%7D%22;%0A%0A%09%09try%0A%09%09%7B%0A%09%09%09new TokenErrorResponseHandler().handleResponse(new StaticMockResponse(HttpStatus.BAD_REQUEST, EmptyHeaders.INSTANCE, new StaticMockResponseEntity(%0A%09%09%09%09new StructuredMediaType(%22application%22, %22json%22, %22utf-8%22), accessTokenResponse)));%0A%09%09%09fail(%22No exception thrown%22);%0A%09%09%7D%0A%09%09catch (TokenRequestError e)%0A%09%09%7B%0A%09%09%09assertEquals(%22invalid_grant%22, e.getMessage());%0A%09%09%09assertEquals(%22%22, e.description());%0A%09%09%09assertEquals(URI.create(%22http://example.com%22), e.uri());%0A%09%09%7D%0A%09%09catch (Exception t)%0A%09%09%7B%0A%09%09%09fail(String.format(%22wrong exception type thrown: %25s%22, t.getClass().getName()));%0A%09%09%7D%0A%09%7D%0A%0A%0A
 %09@Test%0A%09

improve TokenErrorResponseHandlerTest