Search in sources :

Example 1 with StreamResourceRegistry

use of com.vaadin.flow.server.StreamResourceRegistry in project flow by vaadin.

the class StreamResourceRegistryTest method registerTwoResourcesWithSameName_resourcesHasDifferentURI.

@Test
public void registerTwoResourcesWithSameName_resourcesHasDifferentURI() {
    StreamResourceRegistry registry = session.getResourceRegistry();
    StreamResource resource1 = new StreamResource("name", () -> makeEmptyStream());
    StreamRegistration registration1 = registry.registerResource(resource1);
    StreamResource resource2 = new StreamResource("name", () -> makeEmptyStream());
    StreamRegistration registration2 = registry.registerResource(resource2);
    Assert.assertNotEquals("Two different resource are registered to the same URI", registration1.getResourceUri(), registration2.getResourceUri());
    registration1.unregister();
    Assert.assertTrue("Second resource is not found after first resource has been unregistered", registry.getResource(registration2.getResourceUri()).isPresent());
}
Also used : StreamResourceRegistry(com.vaadin.flow.server.StreamResourceRegistry) StreamResource(com.vaadin.flow.server.StreamResource) StreamRegistration(com.vaadin.flow.server.StreamRegistration) Test(org.junit.Test)

Example 2 with StreamResourceRegistry

use of com.vaadin.flow.server.StreamResourceRegistry in project flow by vaadin.

the class StreamResourceRegistryTest method getResourceUrlIsEncoded.

@Test
public void getResourceUrlIsEncoded() throws UnsupportedEncodingException {
    StreamResourceRegistry registry = session.getResourceRegistry();
    StreamResource resource = new StreamResource("a?b=c d&e", () -> makeEmptyStream());
    StreamRegistration registration = registry.registerResource(resource);
    URI url = registration.getResourceUri();
    String suffix = URLEncoder.encode(resource.getName(), StandardCharsets.UTF_8.name());
    Assert.assertTrue("Resource url is not encoded", url.toString().endsWith(suffix));
}
Also used : StreamResourceRegistry(com.vaadin.flow.server.StreamResourceRegistry) StreamResource(com.vaadin.flow.server.StreamResource) StreamRegistration(com.vaadin.flow.server.StreamRegistration) URI(java.net.URI) Test(org.junit.Test)

Example 3 with StreamResourceRegistry

use of com.vaadin.flow.server.StreamResourceRegistry in project flow by vaadin.

the class StreamResourceRegistryTest method unregisterResource_resourceIsRemoved.

@Test
public void unregisterResource_resourceIsRemoved() {
    StreamResourceRegistry registry = session.getResourceRegistry();
    StreamResource resource = new StreamResource("name", () -> makeEmptyStream());
    StreamRegistration registration = registry.registerResource(resource);
    Assert.assertNotNull(registration);
    URI uri = registration.getResourceUri();
    registration.unregister();
    Optional<StreamResource> stored = registry.getResource(StreamResource.class, uri);
    Assert.assertFalse("Unexpected stored resource is found after unregister()", stored.isPresent());
    Assert.assertFalse("Unexpected resource is returned by the registration instance", registration.getResource() != null);
}
Also used : StreamResourceRegistry(com.vaadin.flow.server.StreamResourceRegistry) StreamResource(com.vaadin.flow.server.StreamResource) StreamRegistration(com.vaadin.flow.server.StreamRegistration) URI(java.net.URI) Test(org.junit.Test)

Example 4 with StreamResourceRegistry

use of com.vaadin.flow.server.StreamResourceRegistry in project flow by vaadin.

the class StreamResourceRegistryTest method registerResource_registrationResultCanBeFound.

@Test
public void registerResource_registrationResultCanBeFound() {
    StreamResourceRegistry registry = session.getResourceRegistry();
    StreamResource resource = new StreamResource("name", () -> makeEmptyStream());
    StreamRegistration registration = registry.registerResource(resource);
    Assert.assertNotNull(registration);
    URI uri = registration.getResourceUri();
    Optional<StreamResource> stored = registry.getResource(StreamResource.class, uri);
    Assert.assertSame("Unexpected stored resource is returned for registered URI", resource, stored.get());
    Assert.assertSame("Unexpected resource is returned by the registration instance", resource, registration.getResource());
}
Also used : StreamResourceRegistry(com.vaadin.flow.server.StreamResourceRegistry) StreamResource(com.vaadin.flow.server.StreamResource) StreamRegistration(com.vaadin.flow.server.StreamRegistration) URI(java.net.URI) Test(org.junit.Test)

Aggregations

StreamRegistration (com.vaadin.flow.server.StreamRegistration)4 StreamResource (com.vaadin.flow.server.StreamResource)4 StreamResourceRegistry (com.vaadin.flow.server.StreamResourceRegistry)4 Test (org.junit.Test)4 URI (java.net.URI)3