Skip to content
A simple server for testing HTTP requests powered by SwiftNIO and swift sh
Branch: master
Clone or download
cak Refactor request body capture
- Capture request body in a buffer
- Remove unnecessary connection closes
Latest commit 7fc0998 Mar 27, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
LICENSE
readme.md
server.swift Refactor request body capture Mar 27, 2019

readme.md

server.swift

A simple server for testing HTTP requests powered by SwiftNIO and swift sh.

server.swift supports only SwiftNIO 2 and Swift 5.0

Usage

Install swift sh

brew install mxcl/made/swift-sh

More instructions for swift sh are available at https://github.com/mxcl/swift-sh/.

Run server.swift

swift sh <(curl -L https://github.com/cak/server.swift/raw/master/server.swift)
Starting server.swift on [IPv4]0.0.0.0/0.0.0.0:8000

Alternatively, you can clone the repository and run ‌swift sh server.swift

Command Line Arguments

The hostname and port can be specified with --hostname and --port

swift sh server.swift --hostname "localhost" --port 8080
Starting server.swift on [IPv6]::1/::1:8080

Examples

GET request:

curl -i "http://localhost:8000/get?foo=bar" \
     -H 'origin: https://serversideswift.dev'

Console

GET request to /get?foo=bar from [IPv6]::1/::1:52978
{
  "path" : "\/get?foo=bar",
  "method" : "GET",
  "headers" : {
    "Accept" : "*\/*",
    "Host" : "localhost:8000",
    "origin" : "https:\/\/serversideswift.dev",
    "User-Agent" : "curl\/7.54.0"
  },
  "origin" : "[IPv6]::1\/::1:52978"
}

HTTP Response

HTTP/1.1 200 OK
Server: server.swift
content-type: application/json; charset=utf-8
Content-Length: 244
access-control-allow-origin: https://serversideswift.dev
access-control-allow-headers: accept, authorization, content-type, origin, x-requested-with
access-control-allow-methods: GET, POST, PUT, OPTIONS, DELETE, PATCH
access-control-max-age: 600

{
  "path" : "\/get?foo=bar",
  "method" : "GET",
  "headers" : {
    "Accept" : "*\/*",
    "Host" : "localhost:8000",
    "origin" : "https:\/\/serversideswift.dev",
    "User-Agent" : "curl\/7.54.0"
  },
  "origin" : "[IPv6]::1\/::1:52978"
}

POST request:

curl -i -X "POST" "http://localhost:8000/post" \
     -H 'Content-Type: application/json; charset=utf-8' \
     -d $'{
  "foo": "bar"
}'

Console

POST request to /post from [IPv4]127.0.0.1/127.0.0.1:51299
{
  "path" : "\/post",
  "body" : "{\n  \"foo\": \"bar\"\n}",
  "method" : "POST",
  "headers" : {
    "Content-Type" : "application\/json; charset=utf-8",
    "Host" : "localhost:8000",
    "Accept" : "*\/*",
    "Content-Length" : "18",
    "User-Agent" : "curl\/7.54.0"
  },
  "origin" : "[IPv4]127.0.0.1\/127.0.0.1:51299"
}

HTTP Response

HTTP/1.1 200 OK
Server: server.swift
content-type: application/json; charset=utf-8
Content-Length: 327

{
  "path" : "\/post",
  "body" : "{\n  \"foo\": \"bar\"\n}",
  "method" : "POST",
  "headers" : {
    "Accept" : "*\/*",
    "Host" : "localhost:8000",
    "Content-Length" : "18",
    "Content-Type" : "application\/json; charset=utf-8",
    "User-Agent" : "curl\/7.54.0"
  },
  "origin" : "[IPv4]127.0.0.1\/127.0.0.1:49362"
}

Contributing

Send a pull request or create an issue.

You can’t perform that action at this time.