|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.openqa.selenium.grid.router; |
| 19 | + |
| 20 | +import com.google.common.collect.ImmutableList; |
| 21 | +import com.google.common.collect.ImmutableMap; |
| 22 | +import org.junit.Before; |
| 23 | +import org.junit.Test; |
| 24 | +import org.openqa.selenium.WebDriverInfo; |
| 25 | +import org.openqa.selenium.chrome.ChromeDriverInfo; |
| 26 | +import org.openqa.selenium.events.EventBus; |
| 27 | +import org.openqa.selenium.events.local.GuavaEventBus; |
| 28 | +import org.openqa.selenium.firefox.GeckoDriverInfo; |
| 29 | +import org.openqa.selenium.grid.config.MapConfig; |
| 30 | +import org.openqa.selenium.grid.data.Session; |
| 31 | +import org.openqa.selenium.grid.distributor.Distributor; |
| 32 | +import org.openqa.selenium.grid.distributor.local.LocalDistributor; |
| 33 | +import org.openqa.selenium.grid.node.Node; |
| 34 | +import org.openqa.selenium.grid.node.config.DriverServiceSessionFactory; |
| 35 | +import org.openqa.selenium.grid.node.local.LocalNode; |
| 36 | +import org.openqa.selenium.grid.server.BaseServerOptions; |
| 37 | +import org.openqa.selenium.grid.server.Server; |
| 38 | +import org.openqa.selenium.grid.sessionmap.SessionMap; |
| 39 | +import org.openqa.selenium.grid.sessionmap.local.LocalSessionMap; |
| 40 | +import org.openqa.selenium.grid.testing.TestSessionFactory; |
| 41 | +import org.openqa.selenium.grid.web.EnsureSpecCompliantHeaders; |
| 42 | +import org.openqa.selenium.netty.server.NettyServer; |
| 43 | +import org.openqa.selenium.remote.http.Contents; |
| 44 | +import org.openqa.selenium.remote.http.HttpClient; |
| 45 | +import org.openqa.selenium.remote.http.HttpRequest; |
| 46 | +import org.openqa.selenium.remote.http.HttpResponse; |
| 47 | +import org.openqa.selenium.remote.http.Routable; |
| 48 | +import org.openqa.selenium.remote.service.DriverService; |
| 49 | +import org.openqa.selenium.remote.tracing.DefaultTestTracer; |
| 50 | +import org.openqa.selenium.remote.tracing.Tracer; |
| 51 | +import org.openqa.selenium.testing.drivers.Browser; |
| 52 | + |
| 53 | +import java.net.URI; |
| 54 | +import java.net.URISyntaxException; |
| 55 | + |
| 56 | +import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR; |
| 57 | +import static org.assertj.core.api.Assertions.assertThat; |
| 58 | +import static org.assertj.core.api.Assumptions.assumeThat; |
| 59 | +import static org.openqa.selenium.json.Json.JSON_UTF_8; |
| 60 | +import static org.openqa.selenium.remote.http.HttpMethod.POST; |
| 61 | + |
| 62 | +public class NewSessionCreationTest { |
| 63 | + |
| 64 | + private Tracer tracer; |
| 65 | + private EventBus events; |
| 66 | + private HttpClient.Factory clientFactory; |
| 67 | + |
| 68 | + @Before |
| 69 | + public void setup() { |
| 70 | + tracer = DefaultTestTracer.createTracer(); |
| 71 | + events = new GuavaEventBus(); |
| 72 | + clientFactory = HttpClient.Factory.createDefault(); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void ensureJsCannotCreateANewSession() throws URISyntaxException { |
| 77 | + ChromeDriverInfo chromeDriverInfo = new ChromeDriverInfo(); |
| 78 | + assumeThat(chromeDriverInfo.isAvailable()).isTrue(); |
| 79 | + GeckoDriverInfo geckoDriverInfo = new GeckoDriverInfo(); |
| 80 | + assumeThat(geckoDriverInfo.isAvailable()).isTrue(); |
| 81 | + |
| 82 | + SessionMap sessions = new LocalSessionMap(tracer, events); |
| 83 | + Distributor distributor = new LocalDistributor(tracer, events, clientFactory, sessions, null); |
| 84 | + Routable router = new Router(tracer, clientFactory, sessions, distributor).with(new EnsureSpecCompliantHeaders(ImmutableList.of())); |
| 85 | + |
| 86 | + Server<?> server = new NettyServer( |
| 87 | + new BaseServerOptions(new MapConfig(ImmutableMap.of())), |
| 88 | + router, |
| 89 | + new ProxyCdpIntoGrid(clientFactory, sessions)) |
| 90 | + .start(); |
| 91 | + |
| 92 | + URI uri = server.getUrl().toURI(); |
| 93 | + Node node = LocalNode.builder( |
| 94 | + tracer, |
| 95 | + events, |
| 96 | + uri, |
| 97 | + uri, |
| 98 | + null) |
| 99 | + .add(Browser.detect().getCapabilities(), new TestSessionFactory((id, caps) -> new Session(id, uri, caps))) |
| 100 | + .build(); |
| 101 | + distributor.add(node); |
| 102 | + |
| 103 | + HttpClient client = HttpClient.Factory.createDefault().createClient(server.getUrl()); |
| 104 | + |
| 105 | + // Attempt to create a session without setting the content type |
| 106 | + HttpResponse res = client.execute( |
| 107 | + new HttpRequest(POST, "/session") |
| 108 | + .setContent(Contents.asJson(ImmutableMap.of( |
| 109 | + "capabilities", ImmutableMap.of( |
| 110 | + "alwaysMatch", Browser.detect().getCapabilities()))))); |
| 111 | + |
| 112 | + assertThat(res.getStatus()).isEqualTo(HTTP_INTERNAL_ERROR); |
| 113 | + |
| 114 | + // Attempt to create a session with an origin header but content type set |
| 115 | + res = client.execute( |
| 116 | + new HttpRequest(POST, "/session") |
| 117 | + .addHeader("Content-Type", JSON_UTF_8) |
| 118 | + .addHeader("Origin", "localhost") |
| 119 | + .setContent(Contents.asJson(ImmutableMap.of( |
| 120 | + "capabilities", ImmutableMap.of( |
| 121 | + "alwaysMatch", Browser.detect().getCapabilities()))))); |
| 122 | + |
| 123 | + assertThat(res.getStatus()).isEqualTo(HTTP_INTERNAL_ERROR); |
| 124 | + |
| 125 | + // And now make sure the session is just fine |
| 126 | + res = client.execute( |
| 127 | + new HttpRequest(POST, "/session") |
| 128 | + .addHeader("Content-Type", JSON_UTF_8) |
| 129 | + .setContent(Contents.asJson(ImmutableMap.of( |
| 130 | + "capabilities", ImmutableMap.of( |
| 131 | + "alwaysMatch", Browser.detect().getCapabilities()))))); |
| 132 | + |
| 133 | + assertThat(res.isSuccessful()).isTrue(); |
| 134 | + } |
| 135 | + |
| 136 | + private LocalNode.Builder addDriverFactory( |
| 137 | + LocalNode.Builder builder, |
| 138 | + WebDriverInfo info, |
| 139 | + DriverService.Builder<?, ?> driverService) { |
| 140 | + return builder.add( |
| 141 | + info.getCanonicalCapabilities(), |
| 142 | + new DriverServiceSessionFactory( |
| 143 | + tracer, |
| 144 | + clientFactory, |
| 145 | + info::isSupporting, |
| 146 | + driverService)); |
| 147 | + } |
| 148 | +} |
0 commit comments