diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9d0e32d..d2173fc 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,56 +1,49 @@ name: CI -on: [push, pull_request] +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] -jobs: - format: - name: Format and compile with warnings as errors - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v1 - - - name: Install OTP and Elixir - uses: actions/setup-elixir@v1 - with: - otp-version: 23.0 - elixir-version: 1.11.2 - - - name: Install dependencies - run: mix deps.get - - - name: Run "mix format" - run: mix format --check-formatted - - - name: Compile with --warnings-as-errors - run: mix compile --warnings-as-errors +permissions: + contents: read - test: - name: Test (Elixir ${{matrix.elixir}} | Erlang/OTP ${{matrix.erlang}}) - runs-on: ubuntu-latest +jobs: + mix_check: + name: Elixir ${{ matrix.elixir }} / OTP ${{ matrix.otp }} + runs-on: ubuntu-20.04 strategy: - fail-fast: false matrix: + # Following matrix is created by following https://hexdocs.pm/elixir/compatibility-and-deprecations.html + # Only the most recent 3 versions of Elixir are tested." include: - - erlang: 23.0 - elixir: 1.11.2 - - erlang: 22.2 - elixir: 1.10.0 - - erlang: 22.1 - elixir: 1.9.4 - - erlang: 21.3 - elixir: 1.8.2 - + # Elixir v1.16 + - { elixir: '1.16.x', otp: '26.x' } + - { elixir: '1.16.x', otp: '25.x' } + - { elixir: '1.16.x', otp: '24.x' } + # Elixir v1.15 + - { elixir: '1.15.x', otp: '26.x' } + - { elixir: '1.15.x', otp: '25.x' } + - { elixir: '1.15.x', otp: '24.x' } + # Elixir v1.14 + - { elixir: '1.14.x', otp: '25.x' } + - { elixir: '1.14.x', otp: '24.x' } + - { elixir: '1.14.x', otp: '23.x' } steps: - - uses: actions/checkout@v1 - - - name: Install OTP and Elixir - uses: actions/setup-elixir@v1 - with: - otp-version: ${{matrix.erlang}} - elixir-version: ${{matrix.elixir}} - - - name: Install dependencies - run: mix deps.get - - - name: Run tests - run: mix test --trace + - uses: actions/checkout@v3 + - name: Setup Elixir + uses: erlef/setup-beam@v1 + with: + elixir-version: ${{ matrix.elixir }} + otp-version: ${{ matrix.otp }} + - name: Restore dependencies cache + uses: actions/cache@v3 + with: + path: deps + key: ${{ runner.os }}-elixir_${{ matrix.elixir }}-otp_${{ matrix.otp }}-mix_${{ hashFiles('**/mix.lock') }} + restore-keys: ${{ runner.os }}-elixir_${{ matrix.elixir }}-otp_${{ matrix.otp }}-mix + - name: Install dependencies + run: mix deps.get + - name: Run mix check + run: mix check diff --git a/.gitignore b/.gitignore index 008b35d..64105b7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,26 @@ -/_build -/cover -/deps -/doc +# The directory Mix will write compiled artifacts to. +/_build/ + +# If you run "mix test --cover", coverage assets end up here. +/cover/ + +# The directory Mix downloads your dependencies sources to. +/deps/ + +# Where third-party dependencies like ExDoc output generated docs. +/doc/ + +# Ignore .fetch files in case you like to edit your project deps locally. +/.fetch + +# If the VM crashes, it generates a dump, let's ignore it too. erl_crash.dump + +# Also ignore archive artifacts (built via "mix archive.build"). *.ez + +# Ignore package tarball (built via "mix hex.build"). +some-*.tar + +# Temporary files, for example, from tests. +/tmp/ diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index 3c57100..0000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,6 +0,0 @@ -# Changelog - -## v1.0.0 - - * Only support Elixir versions from 1.8 and later - * Only support Plug versions from 1.0 and later diff --git a/LICENSE.txt b/LICENSE.txt deleted file mode 100644 index 8769699..0000000 --- a/LICENSE.txt +++ /dev/null @@ -1,21 +0,0 @@ -The MIT License (MIT) - -Copyright (c) 2015 Andrea Leopardi - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. diff --git a/README.md b/README.md index 811fbb3..7b928f8 100644 --- a/README.md +++ b/README.md @@ -1,53 +1,26 @@ -# PlugHeartbeat +# PlugProbe -![](https://github.com/whatyouhide/plug_heartbeat/workflows/CI/badge.svg) +[![CI](https://github.com/cozy-elixir/plug_probe/actions/workflows/ci.yml/badge.svg)](https://github.com/cozy-elixir/plug_probe/actions/workflows/ci.yml) +[![Hex.pm](https://img.shields.io/hexpm/v/plug_probe.svg)](https://hex.pm/packages/plug_probe) -A tiny plug for responding to heartbeat requests. +> A plug for responding to HTTP probe requests. ## Installation and docs -[Documentation is available on hex.pm][docs]. - -Add a dependency to your application's `mix.exs` file: +Add `plug_probe` to the list of dependencies in `mix.exs`: ```elixir -defp deps do - [{:plug_heartbeat, "~> 1.0"}] +def deps do + [ + {:plug_probe, } + ] end ``` -then run `mix deps.get`. - ## Usage -Just plug this plug (sorry) in your plug pipeline: - -```elixir -defmodule MyServer do - use Plug.Builder - plug PlugHeartbeat - # ... rest of the pipeline -end -``` - -With this setup, all `GET` and `HEAD` requests to `/heartbeat` will return a -*200 OK* status and `OK` as the body. This path can be customized through the -`:path` option: - -```elixir -defmodule MyServer do - use Plug.Builder - plug PlugHeartbeat, path: "/health" - # ... rest of the pipeline -end -``` - -That's pretty much it, but the [docs][docs] contain further details. +For more information, see the [documentation](https://hexdocs.pm/plug_probe). ## License -MIT © Andrea Leopardi, see the [license file][license]. - - -[license]: LICENSE.txt -[docs]: https://hex.pm/packages/plug_heartbeat +Apache License 2.0 diff --git a/lib/plug_heartbeat.ex b/lib/plug_heartbeat.ex deleted file mode 100644 index 97e1086..0000000 --- a/lib/plug_heartbeat.ex +++ /dev/null @@ -1,70 +0,0 @@ -defmodule PlugHeartbeat do - @default_path "/heartbeat" - - @moduledoc """ - A plug for responding to heartbeat requests. - - This plug responds with a successful status to `GET` or `HEAD` requests at a - specific path so that clients can check if a server is alive. - - The response that this plug sends is a *200 OK* response with body `OK`. By - default, the path that responds to the heartbeat is `#{@default_path}`, but it - can be configured. - - Note that this plug **halts the connection**. This is done so that it can be - plugged near the top of a plug pipeline and catch requests early so that - subsequent plugs don't have the chance to tamper the connection. - Read more about halting connections in the [docs for - `Plug.Builder`](http://hexdocs.pm/plug/Plug.Builder.html). - - ## Options - - The following options can be used when calling `plug PlugHeartbeat`. - - * `:path` - a string expressing the path on which `PlugHeartbeat` will be mounted to - respond to heartbeat requests - * `:json` - a boolean which determines whether the response will be an - `application/json` response (if `true`) or a regular response. - - ## Examples - - defmodule MyServer do - use Plug.Builder - plug PlugHeartbeat - - # ... rest of the pipeline - end - - Using a custom heartbeat path is easy: - - defmodule MyServer do - use Plug.Builder - plug PlugHeartbeat, path: "/health" - - # ... rest of the pipeline - end - - """ - - @behaviour Plug - import Plug.Conn - - def init(opts), - do: Keyword.merge([path: @default_path, json: false], opts) - - def call(%Plug.Conn{} = conn, opts) do - expected_path_info = String.split(opts[:path], "/", trim: true) - - if conn.path_info == expected_path_info and conn.method in ~w(GET HEAD) do - conn |> halt |> response(opts[:json]) - else - conn - end - end - - defp response(conn, false = _json), - do: send_resp(conn, 200, "OK") - - defp response(conn, true = _json), - do: conn |> put_resp_content_type("application/json") |> send_resp(200, "{}") -end diff --git a/lib/plug_probe.ex b/lib/plug_probe.ex new file mode 100644 index 0000000..3a9f207 --- /dev/null +++ b/lib/plug_probe.ex @@ -0,0 +1,88 @@ +defmodule PlugProbe do + @default_path "/probe" + + @moduledoc """ + A plug for responding to HTTP probe requests. + + This plug responds to `GET` or `HEAD` requests at a specific path + (`#{@default_path}` by default), with: + + * status code: `200` + * body: `OK` or `{}` (when `json: true` option is set) + + ## Options + + The following options can be used when calling `plug PlugProbe`. + + * `:path` (string) - specify the path on which `PlugProbe` will be mounted + to respond to probe requests. Default to `#{@default_path}`. + * `:json` (boolean) - specify whether the response will be an + `application/json` response. Default to `false`. + + ## Examples + + In general, probe requests are separated from the functionality of the + application, because of that, it's better to handle them as light as + possible. + + In order to do that, this plug should be placed near the top of a plug + pipeline, then it can match requests early so that subsequent plugs don't + have the chance to tamper the connection. + + For a simple plug pipeline: + + defmodule DemoServer do + use Plug.Builder + + # Put it before any other plugs + plug PlugProbe + + # ... + end + + For a Phoenix endpoint: + + defmodule DemoWeb.Endpoint do + use Phoenix.Endpoint, otp_app: :demo + + # Put it before any other plugs + plug PlugProbe + + # ... + end + + Using a custom probe path is easy: + + defmodule DemoServer do + use Plug.Builder + + # Put it before any other plugs + plug PlugProbe, path: "/heartbeat" + + # ... + end + + """ + + @behaviour Plug + import Plug.Conn + + def init(opts), + do: Keyword.merge([path: @default_path, json: false], opts) + + def call(%Plug.Conn{} = conn, opts) do + expected_path_info = String.split(opts[:path], "/", trim: true) + + if conn.path_info == expected_path_info and conn.method in ~w(GET HEAD) do + conn |> halt() |> response(opts[:json]) + else + conn + end + end + + defp response(conn, false = _json), + do: send_resp(conn, 200, "OK") + + defp response(conn, true = _json), + do: conn |> put_resp_content_type("application/json") |> send_resp(200, "{}") +end diff --git a/mix.exs b/mix.exs index f750396..fc8bf28 100644 --- a/mix.exs +++ b/mix.exs @@ -1,20 +1,22 @@ -defmodule PlugHeartbeat.Mixfile do +defmodule PlugProbe.Mixfile do use Mix.Project - @version "1.0.0" - @github_url "https://github.com/whatyouhide/plug_heartbeat" - @description "A tiny plug for responding to heartbeat requests" + @version "0.1.0" + @description "A plug for responding to HTTP probe requests." + @source_url "https://github.com/cozy-elixir/plug_probe" def project do [ - app: :plug_heartbeat, + app: :plug_probe, version: @version, - elixir: "~> 1.8", + elixir: "~> 1.11", deps: deps(), description: @description, - name: "PlugHeartbeat", - source_url: @github_url, - package: package() + source_url: @source_url, + homepage_url: @source_url, + docs: docs(), + package: package(), + aliases: aliases() ] end @@ -24,16 +26,39 @@ defmodule PlugHeartbeat.Mixfile do defp deps do [ - {:plug, "~> 1.0"}, - {:ex_doc, "~> 0.21", only: :dev} + {:plug, "~> 1.15"}, + {:ex_check, "~> 0.15.0", only: [:dev], runtime: false}, + {:credo, ">= 0.0.0", only: [:dev], runtime: false}, + {:dialyxir, ">= 0.0.0", only: [:dev], runtime: false}, + {:ex_doc, ">= 0.0.0", only: [:dev], runtime: false}, + {:mix_audit, ">= 0.0.0", only: [:dev], runtime: false} + ] + end + + defp docs do + [ + extras: ["README.md"], + main: "readme", + source_url: @source_url, + source_ref: "v#{@version}" ] end defp package do [ - maintainers: ["Andrea Leopardi"], - licenses: ["MIT"], - links: %{"GitHub" => @github_url} + exclude_patterns: [], + licenses: ["Apache-2.0"], + links: %{GitHub: @source_url} ] end + + defp aliases do + [publish: ["hex.publish", "tag"], tag: &tag_release/1] + end + + defp tag_release(_) do + Mix.shell().info("Tagging release as v#{@version}") + System.cmd("git", ["tag", "v#{@version}"]) + System.cmd("git", ["push", "--tags"]) + end end diff --git a/mix.lock b/mix.lock index de5cdbf..321469e 100644 --- a/mix.lock +++ b/mix.lock @@ -1,13 +1,22 @@ %{ - "cowboy": {:hex, :cowboy, "1.0.3", "101a5f826ce9a1ebb2ea53a2cdfffc74736e99807e9e492ecd5f26544c04082b", [:make, :rebar], [{:cowlib, "~> 1.0.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "3a15b753bed9595d4e9dab025220d15b7fa392b33dc8964a667549eb18bd8d64"}, - "cowlib": {:hex, :cowlib, "1.0.1", "175a633c472058bbeb1b238a6409766a48b52b43b7b687ed70f03cf6e854b7d9", [:make], [], "hexpm", "4f6b470c0c4cfc261eeff798b20d8e23a9d88ff595b2b19f4173fb125c23bd53"}, - "earmark": {:hex, :earmark, "1.4.3", "364ca2e9710f6bff494117dbbd53880d84bebb692dafc3a78eb50aa3183f2bfd", [:mix], [], "hexpm", "8cf8a291ebf1c7b9539e3cddb19e9cef066c2441b1640f13c34c1d3cfc825fec"}, - "ex_doc": {:hex, :ex_doc, "0.21.3", "857ec876b35a587c5d9148a2512e952e24c24345552259464b98bfbb883c7b42", [:mix], [{:earmark, "~> 1.4", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "0db1ee8d1547ab4877c5b5dffc6604ef9454e189928d5ba8967d4a58a801f161"}, - "makeup": {:hex, :makeup, "1.0.1", "82f332e461dc6c79dbd82fbe2a9c10d48ed07146f0a478286e590c83c52010b5", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "49736fe5b66a08d8575bf5321d716bac5da20c8e6b97714fec2bcd6febcfa1f8"}, - "makeup_elixir": {:hex, :makeup_elixir, "0.14.0", "cf8b7c66ad1cff4c14679698d532f0b5d45a3968ffbcbfd590339cb57742f1ae", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "d4b316c7222a85bbaa2fd7c6e90e37e953257ad196dc229505137c5e505e9eff"}, - "mime": {:hex, :mime, "1.3.1", "30ce04ab3175b6ad0bdce0035cba77bba68b813d523d1aac73d9781b4d193cf8", [:mix], [], "hexpm", "6cbe761d6a0ca5a31a0931bf4c63204bceb64538e664a8ecf784a9a6f3b875f1"}, - "nimble_parsec": {:hex, :nimble_parsec, "0.5.3", "def21c10a9ed70ce22754fdeea0810dafd53c2db3219a0cd54cf5526377af1c6", [:mix], [], "hexpm", "589b5af56f4afca65217a1f3eb3fee7e79b09c40c742fddc1c312b3ac0b3399f"}, - "plug": {:hex, :plug, "1.10.0", "6508295cbeb4c654860845fb95260737e4a8838d34d115ad76cd487584e2fc4d", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "422a9727e667be1bf5ab1de03be6fa0ad67b775b2d84ed908f3264415ef29d4a"}, - "plug_crypto": {:hex, :plug_crypto, "1.1.2", "bdd187572cc26dbd95b87136290425f2b580a116d3fb1f564216918c9730d227", [:mix], [], "hexpm", "6b8b608f895b6ffcfad49c37c7883e8df98ae19c6a28113b02aa1e9c5b22d6b5"}, - "ranch": {:hex, :ranch, "1.1.0", "f7ed6d97db8c2a27cca85cacbd543558001fc5a355e93a7bff1e9a9065a8545b", [:make], [], "hexpm", "98ade939e63e6567da5dec5bc5bd93cbdc53d53f8b1aa998adec60dc4057f048"}, + "bunt": {:hex, :bunt, "1.0.0", "081c2c665f086849e6d57900292b3a161727ab40431219529f13c4ddcf3e7a44", [:mix], [], "hexpm", "dc5f86aa08a5f6fa6b8096f0735c4e76d54ae5c9fa2c143e5a1fc7c1cd9bb6b5"}, + "credo": {:hex, :credo, "1.7.2", "fdee3a7cb553d8f2e773569181f0a4a2bb7d192e27e325404cc31b354f59d68c", [:mix], [{:bunt, "~> 0.2.1 or ~> 1.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "dd15d6fbc280f6cf9b269f41df4e4992dee6615939653b164ef951f60afcb68e"}, + "dialyxir": {:hex, :dialyxir, "1.4.2", "764a6e8e7a354f0ba95d58418178d486065ead1f69ad89782817c296d0d746a5", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "516603d8067b2fd585319e4b13d3674ad4f314a5902ba8130cd97dc902ce6bbd"}, + "earmark_parser": {:hex, :earmark_parser, "1.4.39", "424642f8335b05bb9eb611aa1564c148a8ee35c9c8a8bba6e129d51a3e3c6769", [:mix], [], "hexpm", "06553a88d1f1846da9ef066b87b57c6f605552cfbe40d20bd8d59cc6bde41944"}, + "erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"}, + "ex_check": {:hex, :ex_check, "0.15.0", "074b94c02de11c37bba1ca82ae5cc4926e6ccee862e57a485b6ba60fca2d8dc1", [:mix], [], "hexpm", "33848031a0c7e4209c3b4369ce154019788b5219956220c35ca5474299fb6a0e"}, + "ex_doc": {:hex, :ex_doc, "0.31.0", "06eb1dfd787445d9cab9a45088405593dd3bb7fe99e097eaa71f37ba80c7a676", [:mix], [{:earmark_parser, "~> 1.4.39", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.1", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "5350cafa6b7f77bdd107aa2199fe277acf29d739aba5aee7e865fc680c62a110"}, + "file_system": {:hex, :file_system, "1.0.0", "b689cc7dcee665f774de94b5a832e578bd7963c8e637ef940cd44327db7de2cd", [:mix], [], "hexpm", "6752092d66aec5a10e662aefeed8ddb9531d79db0bc145bb8c40325ca1d8536d"}, + "jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"}, + "makeup": {:hex, :makeup, "1.1.1", "fa0bc768698053b2b3869fa8a62616501ff9d11a562f3ce39580d60860c3a55e", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "5dc62fbdd0de44de194898b6710692490be74baa02d9d108bc29f007783b0b48"}, + "makeup_elixir": {:hex, :makeup_elixir, "0.16.1", "cc9e3ca312f1cfeccc572b37a09980287e243648108384b97ff2b76e505c3555", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "e127a341ad1b209bd80f7bd1620a15693a9908ed780c3b763bccf7d200c767c6"}, + "makeup_erlang": {:hex, :makeup_erlang, "0.1.3", "d684f4bac8690e70b06eb52dad65d26de2eefa44cd19d64a8095e1417df7c8fd", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "b78dc853d2e670ff6390b605d807263bf606da3c82be37f9d7f68635bd886fc9"}, + "mime": {:hex, :mime, "2.0.5", "dc34c8efd439abe6ae0343edbb8556f4d63f178594894720607772a041b04b02", [:mix], [], "hexpm", "da0d64a365c45bc9935cc5c8a7fc5e49a0e0f9932a761c55d6c52b142780a05c"}, + "mix_audit": {:hex, :mix_audit, "2.1.1", "653aa6d8f291fc4b017aa82bdb79a4017903902ebba57960ef199cbbc8c008a1", [:make, :mix], [{:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:yaml_elixir, "~> 2.9", [hex: :yaml_elixir, repo: "hexpm", optional: false]}], "hexpm", "541990c3ab3a7bb8c4aaa2ce2732a4ae160ad6237e5dcd5ad1564f4f85354db1"}, + "nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"}, + "plug": {:hex, :plug, "1.15.2", "94cf1fa375526f30ff8770837cb804798e0045fd97185f0bb9e5fcd858c792a3", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "02731fa0c2dcb03d8d21a1d941bdbbe99c2946c0db098eee31008e04c6283615"}, + "plug_crypto": {:hex, :plug_crypto, "2.0.0", "77515cc10af06645abbfb5e6ad7a3e9714f805ae118fa1a70205f80d2d70fe73", [:mix], [], "hexpm", "53695bae57cc4e54566d993eb01074e4d894b65a3766f1c43e2c61a1b0f45ea9"}, + "telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"}, + "yamerl": {:hex, :yamerl, "0.10.0", "4ff81fee2f1f6a46f1700c0d880b24d193ddb74bd14ef42cb0bcf46e81ef2f8e", [:rebar3], [], "hexpm", "346adb2963f1051dc837a2364e4acf6eb7d80097c0f53cbdc3046ec8ec4b4e6e"}, + "yaml_elixir": {:hex, :yaml_elixir, "2.9.0", "9a256da867b37b8d2c1ffd5d9de373a4fda77a32a45b452f1708508ba7bbcb53", [:mix], [{:yamerl, "~> 0.10", [hex: :yamerl, repo: "hexpm", optional: false]}], "hexpm", "0cb0e7d4c56f5e99a6253ed1a670ed0e39c13fc45a6da054033928607ac08dfc"}, } diff --git a/test/plug_heartbeat_test.exs b/test/plug_probe.exs similarity index 61% rename from test/plug_heartbeat_test.exs rename to test/plug_probe.exs index fb553d8..c8e4b1c 100644 --- a/test/plug_heartbeat_test.exs +++ b/test/plug_probe.exs @@ -1,77 +1,77 @@ -defmodule PlugHeartbeatTest do +defmodule PlugProbeTest do use ExUnit.Case, async: true use Plug.Test defmodule DefaultPath do use Plug.Router - plug(PlugHeartbeat) - plug(:match) - plug(:dispatch) - match(_, do: send_resp(conn, 200, "match")) + plug PlugProbe + plug :match + plug :dispatch + match _, do: send_resp(conn, 200, "match") end defmodule CustomPath do use Plug.Router - plug(PlugHeartbeat, path: "/custom-beat") - plug(:match) - plug(:dispatch) - match(_, do: send_resp(conn, 200, "match")) + plug PlugProbe, path: "/custom-probe" + plug :match + plug :dispatch + match _, do: send_resp(conn, 200, "match") end defmodule Halted do use Plug.Router - plug(PlugHeartbeat) - plug(:match) - plug(:dispatch) - plug(:body_after) + plug PlugProbe + plug :match + plug :dispatch + plug :body_after defp body_after(conn, _opts), do: %{conn | resp_body: "after"} - match(_, do: send_resp(conn, 200, "match")) + match _, do: send_resp(conn, 200, "match") end defmodule JSON do use Plug.Router - plug(PlugHeartbeat, json: true) - plug(:match) - plug(:dispatch) - match(_, do: send_resp(conn, 200, "match")) + plug PlugProbe, json: true + plug :match + plug :dispatch + match _, do: send_resp(conn, 200, "match") end test "default path" do - conn = conn(:get, "/heartbeat") |> DefaultPath.call([]) + conn = conn(:get, "/probe") |> DefaultPath.call([]) assert conn.status == 200 assert conn.resp_body == "OK" refute conn.resp_body == "after" end test "the connection is halted after the heartbeat (by default)" do - conn = conn(:get, "/heartbeat") |> Halted.call([]) + conn = conn(:get, "/probe") |> Halted.call([]) assert conn.status == 200 assert conn.resp_body == "OK" end test "custom path" do - conn = conn(:get, "/custom-beat") |> CustomPath.call([]) + conn = conn(:get, "/custom-probe") |> CustomPath.call([]) assert conn.status == 200 assert conn.resp_body == "OK" end test "HEAD requests" do - conn = conn(:head, "/heartbeat") |> DefaultPath.call([]) + conn = conn(:head, "/probe") |> DefaultPath.call([]) assert conn.status == 200 assert conn.resp_body == "" end test "only GET and HEAD requests work" do Enum.each([:post, :put, :delete, :options, :foo], fn method -> - conn = conn(method, "/heartbeat") |> DefaultPath.call([]) + conn = conn(method, "/probe") |> DefaultPath.call([]) assert conn.resp_body == "match" end) end test "JSON heartbeat" do - conn = conn(:get, "/heartbeat") |> JSON.call([]) + conn = conn(:get, "/probe") |> JSON.call([]) assert conn.resp_body == "{}" assert conn |> get_resp_header("content-type") |> hd =~ "application/json" end