Giter Site home page Giter Site logo

binarybirds / vapor-spec Goto Github PK

View Code? Open in Web Editor NEW
36.0 2.0 2.0 20 KB

Unit testing Vapor applications through declarative specifications.

Home Page: https://binarybirds.com

License: MIT License

Swift 99.71% Makefile 0.29%
vapor-4 unit-test tdd xctest xctvapor vapor

vapor-spec's Introduction

VaporSpec

Unit testing Vapor applications through declarative specifications.

Install

Add the repository as a dependency:

.package(url: "https://github.com/binarybirds/vapor-spec", from: "2.0.0"),

Add VaporSpec to the target dependencies:

.product(name: "VaporSpec", package: "vapor-spec"),

Update the packages and you are ready.

Usage example

Api

import XCTest
@testable import VaporSpec

final class VaporSpecTests: XCTestCase {

    func testStatusCode() throws {
        let app = Application(.testing)
        defer { app.shutdown() }

        try app
            .spec()
            .get("foo")
            .expect(.notFound)
            .test()
    }
    
    func testHeaderValues() throws {
        let app = Application(.testing)
        app.routes.get("hello") { _ in "hello" }
        defer { app.shutdown() }

        try app
            .spec()
            .get("hello")
            .expect("Content-Length", ["5"])
            .expect("Content-Type", ["text/plain; charset=utf-8"])
            .test()
    }
    
    func testContentTypeHeader() throws {
        let app = Application(.testing)
        app.routes.get("hello") { _ in "hello" }
        defer { app.shutdown() }

        try app
            .spec()
            .get("hello")
            .expect("text/plain; charset=utf-8")
            .test()
    }
    
    func testBodyValue() throws {
        let app = Application(.testing)
        app.routes.get("hello") { _ in "hello" }
        defer { app.shutdown() }

        try app
            .spec()
            .get("hello")
            .expect(closure: { res in
                let string = res.body.string
                XCTAssertEqual(string, "hello")
            })
            .test()
    }
    
    func testJSON() throws {
        struct Test: Codable, Content {
            let foo: String
            let bar: Int
            let baz: Bool
        }

        let app = Application(.testing)
        app.routes.post("foo") { req in try req.content.decode(Test.self) }
        defer { app.shutdown() }

        let input = Test(foo: "foo", bar: 42, baz: true)
        try app
            .spec()
            .post("foo")
            .json(input, Test.self) { res in
                XCTAssertEqual(input.foo, res.foo)
                XCTAssertEqual(input.bar, res.bar)
                XCTAssertEqual(input.baz, res.baz)
            }
            .test()
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.