package org.jscookie.test.unit;

import org.jscookie.Cookies;
import org.jscookie.test.unit.utils.BaseTest;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith( MockitoJUnitRunner.class )
public class CookiesEncodingTest extends BaseTest {
	private Cookies cookies;

	@Before
	public void before() {
		cookies = Cookies.initFromServlet( request, response );
	}

	@Test
	public void character_not_allowed_in_name_and_value() {
		cookies.set( ";,\\\" ", ";,\\\" " );
		Mockito.verify( response ).addHeader( "Set-Cookie", "%3B%2C%5C%22%20=%3B%2C%5C%22%20; Path=/" );
	}

	@Test
	public void characters_allowed_in_name_and_value() {
		cookies.set(
			"!#$&'*+-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`abcdefghijklmnopqrstuvwxyz|~",
			"!#$&'*+-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`abcdefghijklmnopqrstuvwxyz|~"
		);
		Mockito.verify( response ).addHeader(
			"Set-Cookie",
			"!#$&'*+-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`abcdefghijklmnopqrstuvwxyz|~=!#$&'*+-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`abcdefghijklmnopqrstuvwxyz|~; Path=/"
		);
	}

	@Test
	public void character_with_3_bytes_in_value() {
		cookies.set( "c", "京" );
		Mockito.verify( response ).addHeader( "Set-Cookie", "c=%E4%BA%AC; Path=/" );
	}

	@Test
	public void characters_allowed_in_cookie_value() {
		cookies.set( "c", "/:<=>?@[]{}" );
		Mockito.verify( response ).addHeader( "Set-Cookie", "c=/:<=>?@[]{}; Path=/" );
	}
}

@@ -1312,32 +1312,197 @@
  Path=/%22 );%0A%09%7D%0A%0A
+%09@Test%0A%09public void character_with_3_bytes_in_name() %7B%0A%09%09cookies.set( %22%E4%BA%AC%22, %22v%22 );%0A%09%09Mockito.verify( response ).addHeader( %22Set-Cookie%22, %22%25E4%25BA%25AC=v; Path=/%22 );%0A%09%7D%0A%0A
 %09@Test%0A%09public v

Test character with 3 bytes in name