Search in sources :

Example 1 with JavaScriptBootstrapUI

use of com.vaadin.flow.component.internal.JavaScriptBootstrapUI in project flow by vaadin.

the class UidlRequestHandler method writeUidl.

void writeUidl(UI ui, Writer writer, boolean resync) throws IOException {
    JsonObject uidl = createUidl(ui, resync);
    if (ui instanceof JavaScriptBootstrapUI) {
        removeOffendingMprHashFragment(uidl);
    }
    // some dirt to prevent cross site scripting
    String responseString = "for(;;);[" + uidl.toJson() + "]";
    writer.write(responseString);
}
Also used : JsonObject(elemental.json.JsonObject) JavaScriptBootstrapUI(com.vaadin.flow.component.internal.JavaScriptBootstrapUI)

Example 2 with JavaScriptBootstrapUI

use of com.vaadin.flow.component.internal.JavaScriptBootstrapUI in project flow by vaadin.

the class JavaScriptBootstrapHandlerTest method should_attachViewTo_Body_when_serverRouting.

@Test
public void should_attachViewTo_Body_when_serverRouting() throws Exception {
    VaadinRequest request = mocks.createRequest(mocks, "/", "v-r=init&location=bar%3Fpar1%26par2&serverSideRouting");
    jsInitHandler.handleRequest(session, request, response);
    JavaScriptBootstrapUI ui = (JavaScriptBootstrapUI) UI.getCurrent();
    TestNodeVisitor visitor = new TestNodeVisitor(true);
    BasicElementStateProvider.get().visit(ui.getElement().getNode(), visitor);
    Assert.assertTrue(hasNodeTag(visitor, "^<body>.*", ElementType.REGULAR));
    Assert.assertTrue(hasNodeTag(visitor, "^<div>.*", ElementType.REGULAR));
    Assert.assertTrue(hasNodeTag(visitor, "^<div>.*Could not navigate to 'bar'.*", ElementType.REGULAR));
    Mockito.verify(session, Mockito.times(1)).setAttribute(SERVER_ROUTING, Boolean.TRUE);
}
Also used : TestNodeVisitor(com.vaadin.flow.dom.TestNodeVisitor) VaadinRequest(com.vaadin.flow.server.VaadinRequest) JavaScriptBootstrapUI(com.vaadin.flow.component.internal.JavaScriptBootstrapUI) Test(org.junit.Test)

Example 3 with JavaScriptBootstrapUI

use of com.vaadin.flow.component.internal.JavaScriptBootstrapUI in project flow by vaadin.

the class UidlRequestHandlerTest method should_updateHash_when_v7LocationNotProvided.

@Test
public void should_updateHash_when_v7LocationNotProvided() throws Exception {
    JavaScriptBootstrapUI ui = mock(JavaScriptBootstrapUI.class);
    UidlRequestHandler handler = spy(new UidlRequestHandler());
    StringWriter writer = new StringWriter();
    JsonObject uidl = generateUidl(false, true);
    doReturn(uidl).when(handler).createUidl(ui, false);
    handler.writeUidl(ui, writer, false);
    String out = writer.toString();
    uidl = JsonUtil.parse(out.substring(9, out.length() - 1));
    assertEquals("setTimeout(() => history.pushState(null, null, location.pathname + location.search + '#!away'));", uidl.getArray("execute").getArray(1).getString(1));
}
Also used : StringWriter(java.io.StringWriter) JsonObject(elemental.json.JsonObject) JavaScriptBootstrapUI(com.vaadin.flow.component.internal.JavaScriptBootstrapUI) Test(org.junit.Test)

Example 4 with JavaScriptBootstrapUI

use of com.vaadin.flow.component.internal.JavaScriptBootstrapUI in project flow by vaadin.

the class UidlRequestHandlerTest method should_modifyUidl_when_MPR_JavaScriptBootstrapUI.

@Test
public void should_modifyUidl_when_MPR_JavaScriptBootstrapUI() throws Exception {
    JavaScriptBootstrapUI ui = mock(JavaScriptBootstrapUI.class);
    UidlRequestHandler handler = spy(new UidlRequestHandler());
    StringWriter writer = new StringWriter();
    JsonObject uidl = generateUidl(true, true);
    doReturn(uidl).when(handler).createUidl(ui, false);
    handler.writeUidl(ui, writer, false);
    String out = writer.toString();
    uidl = JsonUtil.parse(out.substring(9, out.length() - 1));
    String v7Uidl = uidl.getArray("execute").getArray(2).getString(1);
    assertFalse(v7Uidl.contains("http://localhost:9998/#!away"));
    assertTrue(v7Uidl.contains("http://localhost:9998/"));
    assertFalse(v7Uidl.contains("window.location.hash = '!away';"));
}
Also used : StringWriter(java.io.StringWriter) JsonObject(elemental.json.JsonObject) JavaScriptBootstrapUI(com.vaadin.flow.component.internal.JavaScriptBootstrapUI) Test(org.junit.Test)

Example 5 with JavaScriptBootstrapUI

use of com.vaadin.flow.component.internal.JavaScriptBootstrapUI in project flow by vaadin.

the class UidlRequestHandlerTest method should_not_modifyUidl_when_MPR_nonJavaScriptBootstrapUI.

@Test
public void should_not_modifyUidl_when_MPR_nonJavaScriptBootstrapUI() throws Exception {
    JavaScriptBootstrapUI ui = null;
    UidlRequestHandler handler = spy(new UidlRequestHandler());
    StringWriter writer = new StringWriter();
    JsonObject uidl = generateUidl(true, true);
    doReturn(uidl).when(handler).createUidl(ui, false);
    handler.writeUidl(ui, writer, false);
    String out = writer.toString();
    assertTrue(out.startsWith("for(;;);[{"));
    assertTrue(out.endsWith("}]"));
    uidl = JsonUtil.parse(out.substring(9, out.length() - 1));
    String v7Uidl = uidl.getArray("execute").getArray(2).getString(1);
    assertTrue(v7Uidl.contains("http://localhost:9998/#!away"));
    assertTrue(v7Uidl.contains("window.location.hash = '!away';"));
    assertEquals("setTimeout(() => window.history.pushState(null, '', $0))", uidl.getArray("execute").getArray(1).getString(1));
}
Also used : StringWriter(java.io.StringWriter) JsonObject(elemental.json.JsonObject) JavaScriptBootstrapUI(com.vaadin.flow.component.internal.JavaScriptBootstrapUI) Test(org.junit.Test)

Aggregations

JavaScriptBootstrapUI (com.vaadin.flow.component.internal.JavaScriptBootstrapUI)8 Test (org.junit.Test)7 JsonObject (elemental.json.JsonObject)6 StringWriter (java.io.StringWriter)5 TestNodeVisitor (com.vaadin.flow.dom.TestNodeVisitor)2 VaadinRequest (com.vaadin.flow.server.VaadinRequest)2