package io.spacedog.client.http;

import org.junit.Assert;
import org.junit.Test;

public class SpaceBackendTest extends Assert {

	@Test
	public void testIsBackendIdValid() {

		// valid backend ids
		assertTrue(SpaceBackend.isValid("a1234"));
		assertTrue(SpaceBackend.isValid("abcd"));
		assertTrue(SpaceBackend.isValid("1a2b34d"));

		// invalid backend ids
		assertFalse(SpaceBackend.isValid("a"));
		assertFalse(SpaceBackend.isValid("bb"));
		assertFalse(SpaceBackend.isValid("ccc"));
		assertFalse(SpaceBackend.isValid("dd-dd"));
		assertFalse(SpaceBackend.isValid("/eeee"));
		assertFalse(SpaceBackend.isValid("eeee:"));
		assertFalse(SpaceBackend.isValid("abcdE"));
		assertFalse(SpaceBackend.isValid("spacedog"));
		assertFalse(SpaceBackend.isValid("spacedog123"));
		assertFalse(SpaceBackend.isValid("123spacedog"));
		assertFalse(SpaceBackend.isValid("123spacedog123"));
	}

	@Test
	public void testFromDefaults() {
		SpaceBackend backend = SpaceBackend.fromDefaults("production");
		assertEquals("api.spacedog.io", backend.host());
		assertEquals(443, backend.port());
		assertEquals("https", backend.scheme());
		assertEquals("spacedog", backend.backendId());
		assertTrue(backend.ssl());
		assertTrue(backend.isMulti());
		assertEquals("https://*.spacedog.io", backend.toString());
		assertEquals("https://api.spacedog.io/index.html", backend.url("/index.html"));
	}

	@Test
	public void testInstanciate() {
		SpaceBackend backend = SpaceBackend.production.fromBackendId("test");
		assertEquals("test.spacedog.io", backend.host());
		assertEquals(443, backend.port());
		assertEquals("https", backend.scheme());
		assertEquals("test", backend.backendId());
		assertTrue(backend.ssl());
		assertFalse(backend.isMulti());
		assertEquals("https://test.spacedog.io", backend.toString());
		assertEquals("https://test.spacedog.io/index.html", backend.url("/index.html"));
	}

	@Test
	public void testFromUrlMonoApiBackend() {
		SpaceBackend backend = SpaceBackend.fromUrl("http://connect.acme.net");
		assertEquals("connect.acme.net", backend.host());
		assertEquals(80, backend.port());
		assertEquals("http", backend.scheme());
		assertEquals("spacedog", backend.backendId());
		assertFalse(backend.ssl());
		assertFalse(backend.isMulti());
		assertEquals("http://connect.acme.net", backend.toString());
		assertEquals("http://connect.acme.net/index.html", backend.url("/index.html"));
	}

	@Test
	public void testFromUrlMultiWebAppBackend() {
		SpaceBackend backend = SpaceBackend.fromUrl("http://www.*.acme.net:8080");
		assertEquals("www.api.acme.net", backend.host());
		assertEquals(8080, backend.port());
		assertEquals("http", backend.scheme());
		assertEquals("spacedog", backend.backendId());
		assertFalse(backend.ssl());
		assertTrue(backend.isMulti());
		assertEquals("http://www.*.acme.net:8080", backend.toString());
		assertEquals("http://www.api.acme.net:8080/index.html", backend.url("/index.html"));
	}

	@Test
	public void apiMultiBackendCanHandleRequests() {
		SpaceBackend backend = SpaceBackend.fromDefaults("production")//
				.fromRequest("test.spacedog.io").get();

		assertEquals("test.spacedog.io", backend.host());
		assertEquals("https://test.spacedog.io", backend.toString());
		assertEquals(443, backend.port());
		assertEquals("https", backend.scheme());
		assertEquals("test", backend.backendId());
		assertTrue(backend.ssl());
		assertFalse(backend.isMulti());

		try {
			backend.fromBackendId("test2");
			fail();

		} catch (IllegalArgumentException ignored) {
		}
	}

	@Test
	public void apiMultiBackendCanNotHandleWebAppRequest() {
		assertFalse(SpaceBackend.fromDefaults("production")//
				.fromRequest("test.www.spacedog.io")//
				.isPresent());
	}

	@Test
	public void webAppMultiBackendCanHandleWebAppRequest() {
		SpaceBackend backend = SpaceBackend.fromUrl("https://*.www.spacedog.io")//
				.fromRequest("test.www.spacedog.io").get();

		assertEquals("test.www.spacedog.io", backend.host());
		assertEquals(443, backend.port());
		assertEquals("https", backend.scheme());
		assertEquals("test", backend.backendId());
		assertTrue(backend.ssl());
		assertFalse(backend.isMulti());
		assertEquals("https://test.www.spacedog.io", backend.toString());
		assertEquals("https://test.www.spacedog.io/index.html", backend.url("/index.html"));
	}

	@Test
	public void api_backend_id_is_special() {

		SpaceBackend prod = SpaceBackend.fromDefaults("production");
		SpaceBackend backend = prod.fromRequest("api.spacedog.io").get();

		assertEquals(prod, backend);

		assertEquals("api.spacedog.io", backend.host());
		assertEquals("https://*.spacedog.io", backend.toString());
		assertEquals(443, backend.port());
		assertEquals("https", backend.scheme());
		assertEquals("api", backend.backendId());
		assertTrue(backend.ssl());
		assertTrue(backend.isMulti());
	}

	@Test
	public void spacedog_backend_id_is_special() {

		SpaceBackend prod = SpaceBackend.fromDefaults("production");
		SpaceBackend backend = prod.fromRequest("spacedog.spacedog.io").get();

		assertEquals(prod, backend);

		assertEquals("api.spacedog.io", backend.host());
		assertEquals("https://*.spacedog.io", backend.toString());
		assertEquals(443, backend.port());
		assertEquals("https", backend.scheme());
		assertEquals("spacedog", backend.backendId());
		assertTrue(backend.ssl());
		assertTrue(backend.isMulti());
	}
}

@@ -4715,19 +4715,24 @@
 Equals(%22
-api
+spacedog
 %22, backe
@@ -4806,24 +4806,98 @@
 isMulti());%0A
+%09%09assertEquals(%22https://api.spacedog.io/1/data%22, backend.url(%22/1/data%22));%0A
 %09%7D%0A%0A%09@Test%0A%09
@@ -5416,13 +5416,350 @@
 lti());%0A
+%09%09assertEquals(%22https://api.spacedog.io/1/data%22, backend.url(%22/1/data%22));%0A%09%7D%0A%0A%09@Test%0A%09public void testInstanciateWithApiOrSpacedogBackendId() %7B%0A%09%09assertEquals(SpaceBackend.production, //%0A%09%09%09%09SpaceBackend.production.fromBackendId(%22api%22));%0A%09%09assertEquals(SpaceBackend.production, //%0A%09%09%09%09SpaceBackend.production.fromBackendId(%22spacedog%22));%0A
 %09%7D%0A%7D%0A

Fix SpaceBackend class tests