This document will take you through the steps to test Kitura-Websockets using autobahn-testsuite.
These tests are run against a WebSocket EchoServer so we must first set one up.
- Create a swift project:
mkdir ~/WebSocketEchoServer
cd ~/WebSocketEchoServer
swift package init --type executable
open Package.swift
- Inside the
Package.swift
replace the text with:
// swift-tools-version:4.0
import PackageDescription
let package = Package(
name: "WebSocketEchoServer",
dependencies: [
.package(url: "https://github.com/IBM-Swift/Kitura.git", from: "2.3.0"),
.package(url: "https://github.com/IBM-Swift/HeliumLogger.git", .upToNextMinor(from: "1.7.0")),
.package(url: "https://github.com/IBM-Swift/Kitura-WebSocket.git", from: "2.0.0")
],
targets: [
.target(
name: "WebSocketEchoServer",
dependencies: ["Kitura", "HeliumLogger", "Kitura-WebSocket"]),
]
)
- Open the projects main.swift file:
open ~/WebSocketEchoServer/Sources/WebSocketEchoServer/main.swift
- Inside the
main.swift
file replaceprint("Hello, world!")
with:
import Foundation
import Kitura
import KituraWebSocket
import HeliumLogger
// Using an implementation for a Logger
HeliumLogger.use(.info)
let router = Router()
WebSocket.register(service: EchoService(), onPath: "/")
let port = 9001
Kitura.addHTTPServer(onPort: port, with: router)
Kitura.run()
- Create the
EchoService
file
cd ~/WebSocketEchoServer/Sources/WebSocketEchoServer/
touch EchoService.swift
open EchoService.swift
- Inside
EchoService
add the following code:
import Foundation
import KituraWebSocket
import LoggerAPI
class EchoService: WebSocketService {
public func connected(connection: WebSocketConnection) {}
public func disconnected(connection: WebSocketConnection, reason: WebSocketCloseReasonCode) {}
public func received(message: Data, from: WebSocketConnection) {
from.send(message: message)
}
public func received(message: String, from: WebSocketConnection) {
Log.info("Got message \(message)... sending it back")
from.send(message: message)
}
}
- Start the EchoServer:
cd ~/WebSocketEchoServer
swift build
.build/x86_64-apple-macosx10.10/debug/WebSocketEchoServer
Open a new terminal window and enter:
pip install autobahntestsuite
cd ~
mkdir testAutobahn
cd testAutobahn
wstest -m fuzzingclient
This will run the autobahn tests against a server running on port 9001.
To change the tests inside testAutobahn
:
open fuzzingclient.json
In here you can select specific tests to include or exclude.
To view the results open ~/testAutobahn/reports/servers/index.html