ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - xavier/plug_test_helpers: A simple testing DSL for Plugs
A simple testing DSL for Plugs. Contribute to xavier/plug_test_helpers development by creating an account on GitHub.
Visit Site

GitHub - xavier/plug_test_helpers: A simple testing DSL for Plugs

GitHub - xavier/plug_test_helpers: A simple testing DSL for Plugs

Plug Test Helpers

Simple helpers to test your Plugs with ExUnit. Still an experiment in progress at this point.

The library provides a set of macros which can perform assertions on Plug.Conn structs to verify the response status, headers and body.

Installation

Add the dependency in your mix.exs file:

def deps do
  [ { :plug_test_helpers, "~> 0.1" } ]
end

After you are done, run mix deps.get in your shell to get install it.

Usage

Add use PlugTestHelpers to your test case to bring in new Plug-specific assert macros :


defmodule do
  use ExUnit.Case, async: true

  use Plug.Test
  use PlugTestHelpers

  @opts MyPlug.init([])

  test "status" do
    conn = conn(:get, "/")
    conn = MyPlug.call(conn, @opts)
    assert_status 200
  end

  test "404" do
    conn = conn(:get, "/not-found")
    conn = MyPlug.call(conn, @opts)
    # Works with symbols toos
    assert_status :not_found
  end

  test "redirect" do
    conn = conn(:get, "/redirect")
    conn = MyPlug.call(conn, @opts)
    # Will check both HTTP status code and header
    assert_redirect "http://example.com"
  end

  test "headers" do
    conn = conn(:get, "/image.jpg")
    conn = MyPlug.call(conn, @opts)
    # Will pass if the header is present
    assert_header "content_type"
    # Will pass if the header value is set to the given string
    assert_header "content_type", "image/jpg"
    # Will pass if the header value matches the given regex
    assert_header_match "content_type", ~r/\Aimage\/jpe?g\Z/
  end

  test "body" do
    conn = conn(:get, "/")
    conn = MyPlug.call(conn, @opts)
    # Will pass if the response body is the given string
    assert_body "complete response text"
    # Will pass if the response body matches the given regex
    assert_body_match ~r/complete/
  end

end

Current Design Decisions

Things may always change but these are the current design decisions taken to build this library.

Convention over Configuration

Each of the assert macros expect a variable named conn to be bound to a Plug.Conn.

This is definitely a questionable design decision and I'd be very happy ot get some feedback about this.

I think it's nice to have tests that are simple and concise. Is it worth providing macros accepting an extra conn parameter?

Advanced Response Body Assertions

Assertions to match against structured response content (such as HTML or JSON) are currently out of the scope of this library.

License

Copyright 2014 Xavier Defrang

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory