DefaultSameOriginPolicy implements SameOriginPolicy { @Override public boolean isSameOrigin(ContainerRequestContext requestContext, String origin) { URI requestUri = requestContext.getUriInfo().getRequestUri(); StringBuilder target = new StringBuilder(); String scheme = requestUri.getScheme(); if (scheme == null) { return false; } scheme = scheme.toLowerCase(Locale.ENGLISH); target.append(scheme); target.append(": String host = requestUri.getHost(); if (host == null) { return false; } target.append(host); int port = requestUri.getPort(); if (port != -1 && !isDefaultPort(scheme, port)) { target.append(":"); target.append(port); } return origin.equalsIgnoreCase(target.toString()); }  @Override boolean isSameOrigin(ContainerRequestContext requestContext, String origin);  }
@Test public void requestUriWithoutSchemeGiven_Fails() { assertFalse(new DefaultSameOriginPolicy().isSameOrigin(createContainerRequestContext("api.example.com"), "https: }
@Test public void requestUriWithoutHostGiven_Fails() { assertFalse(new DefaultSameOriginPolicy().isSameOrigin(createContainerRequestContext("http: }
@Test public void requestUriWithHttpSchemeAndPort80Given_IgnoresPort() { assertTrue(new DefaultSameOriginPolicy().isSameOrigin(createContainerRequestContext("http: assertFalse(new DefaultSameOriginPolicy().isSameOrigin(createContainerRequestContext("http: }
@Test public void requestUriWithHttpSchemeAndNoPortGiven_IgnoresPort() { assertTrue(new DefaultSameOriginPolicy().isSameOrigin(createContainerRequestContext("http: assertFalse(new DefaultSameOriginPolicy().isSameOrigin(createContainerRequestContext("http: }
@Test public void requestUriWithHttpSchemeAndNonDefaultPortGiven_ConsidersPort() { assertFalse(new DefaultSameOriginPolicy().isSameOrigin(createContainerRequestContext("http: assertTrue(new DefaultSameOriginPolicy().isSameOrigin(createContainerRequestContext("http: }
@Test public void requestUriWithHttpsSchemeAndDefaultPortGiven_IgnoresPort() { assertTrue(new DefaultSameOriginPolicy().isSameOrigin(createContainerRequestContext("https: assertFalse(new DefaultSameOriginPolicy().isSameOrigin(createContainerRequestContext("https: }
@Test public void requestUriWithHttpsSchemeAndNoPortGiven_IgnoresPort() { assertTrue(new DefaultSameOriginPolicy().isSameOrigin(createContainerRequestContext("https: assertFalse(new DefaultSameOriginPolicy().isSameOrigin(createContainerRequestContext("https: }
@Test public void requestUriWithHttpsSchemeAndNonDefaultPortGiven_ConsidersPort() { assertFalse(new DefaultSameOriginPolicy().isSameOrigin(createContainerRequestContext("https: assertTrue(new DefaultSameOriginPolicy().isSameOrigin(createContainerRequestContext("https: }
@Test public void subdomainGiven_NoMatch() { assertFalse(new DefaultSameOriginPolicy().isSameOrigin(createContainerRequestContext("https: assertFalse(new DefaultSameOriginPolicy().isSameOrigin(createContainerRequestContext("https: }