Skip to content

Commit

Permalink
Fixes lagom#2135, stop sending duplicate Sec-WebSocket-Protocol heade…
Browse files Browse the repository at this point in the history
…r in response when a subprotocol is defined
  • Loading branch information
borgespires committed Jan 27, 2020
1 parent bf71f88 commit 7970ccb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
Expand Up @@ -181,6 +181,8 @@ class AkkaHttpServiceGateway(
"Sec-WebSocket-Accept",
"Sec-WebSocket-Version",
"Sec-WebSocket-Key",
"Sec-WebSocket-Extensions",
"Sec-WebSocket-Protocol",
"UpgradeToWebSocket",
"Upgrade",
"Connection",
Expand Down
Expand Up @@ -14,11 +14,10 @@ import akka.http.scaladsl.model.ws.Message
import akka.http.scaladsl.model.ws.TextMessage
import akka.http.scaladsl.model.ws.UpgradeToWebSocket
import akka.http.scaladsl.model.ws.WebSocketRequest
import akka.http.scaladsl.model.HttpEntity
import akka.http.scaladsl.model.HttpRequest
import akka.http.scaladsl.model.HttpResponse
import akka.http.scaladsl.model._
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.Flow
import akka.stream.scaladsl.Keep
import akka.stream.scaladsl.Sink
import akka.stream.scaladsl.Source
import akka.util.ByteString
Expand Down Expand Up @@ -54,7 +53,17 @@ class AkkaHttpServiceGatewaySpec extends WordSpec with Matchers with BeforeAndAf
case req if req.uri.path.toString() == "/echo-headers" =>
HttpResponse(entity = HttpEntity(req.headers.map(h => s"${h.name()}: ${h.value}").mkString("\n")))
case stream if stream.uri.path.toString() == "/stream" =>
stream.header[UpgradeToWebSocket].get.handleMessages(Flow[Message])
stream
.header[UpgradeToWebSocket]
.get
.handleMessages(
Flow[Message],
stream.headers
.find(_.lowercaseName() == "sec-websocket-protocol")
.map(_.value)
.map(_.split(","))
.map(_.head)
)
},
"localhost",
port = 0
Expand Down Expand Up @@ -118,6 +127,30 @@ class AkkaHttpServiceGatewaySpec extends WordSpec with Matchers with BeforeAndAf
(result should contain).inOrderOnly("Hello", "world")
}

"serve websocket requests with the correct response" in {
val flow = http.webSocketClientFlow(
WebSocketRequest(
s"$gatewayWsUri/stream",
collection.immutable.Seq.empty[HttpHeader],
collection.immutable.Seq("echo")
)
)
val result = Await.result(
Source
.single(TextMessage("hello world!"))
.viaMat(flow)(Keep.right)
.to(Sink.ignore)
.run(),
10.seconds
)

result.response.status should equal(StatusCodes.SwitchingProtocols)
result.response.headers.count(_.lowercaseName() == "sec-websocket-protocol") should equal(1)
result.response.headers.find(_.lowercaseName() == "sec-websocket-protocol").map(_.value()).get should equal(
"echo"
)
}

"serve not found when no ACL matches" in {
val response = Await.result(http.singleRequest(HttpRequest(uri = s"$gatewayUri/notfound")), 10.seconds)
response.status.intValue() should ===(404)
Expand Down

0 comments on commit 7970ccb

Please sign in to comment.