Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A simple testing DSL for Plugs

License

NotificationsYou must be signed in to change notification settings

xavier/plug_test_helpers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

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

Installation

Add the dependency in yourmix.exs file:

defdepsdo[{:plug_test_helpers,"~> 0.1"}]end

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

Usage

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

defmoduledouseExUnit.Case,async:trueusePlug.TestusePlugTestHelpers@optsMyPlug.init([])test"status"doconn=conn(:get,"/")conn=MyPlug.call(conn,@opts)assert_status200endtest"404"doconn=conn(:get,"/not-found")conn=MyPlug.call(conn,@opts)# Works with symbols toosassert_status:not_foundendtest"redirect"doconn=conn(:get,"/redirect")conn=MyPlug.call(conn,@opts)# Will check both HTTP status code and headerassert_redirect"http://example.com"endtest"headers"doconn=conn(:get,"/image.jpg")conn=MyPlug.call(conn,@opts)# Will pass if the header is presentassert_header"content_type"# Will pass if the header value is set to the given stringassert_header"content_type","image/jpg"# Will pass if the header value matches the given regexassert_header_match"content_type",~r/\Aimage\/jpe?g\Z/endtest"body"doconn=conn(:get,"/")conn=MyPlug.call(conn,@opts)# Will pass if the response body is the given stringassert_body"complete response text"# Will pass if the response body matches the given regexassert_body_match~r/complete/endend

Current Design Decisions

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

Convention over Configuration

Each of theassert macros expect a variable namedconn to be bound to aPlug.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 extraconn 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.

About

A simple testing DSL for Plugs

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp