From 5b0bf830523930db557f915d5cdc4e7dde0a107a Mon Sep 17 00:00:00 2001 From: Jordan Bracco Date: Tue, 16 Jun 2020 11:57:01 +0200 Subject: [PATCH] Add Plug to changelog & format --- CHANGELOG.md | 1 + test/builds-sr-ht.exs | 52 ++++++++++++++++++++++++++++++++++++++++ test/majic/plug_test.exs | 40 ++++++++++++++++++++----------- 3 files changed, 79 insertions(+), 14 deletions(-) create mode 100644 test/builds-sr-ht.exs diff --git a/CHANGELOG.md b/CHANGELOG.md index c49c78a..bebb05d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ The format is based on [Keep a Changelog][1], and this project adheres to [Seman - Forked gen_magic. - Pool: `Majic.Pool` +- Plug: `Majic.Plug` - Unified API: `Majic.perform/1,2,3` ## Changed diff --git a/test/builds-sr-ht.exs b/test/builds-sr-ht.exs new file mode 100644 index 0000000..7f7c7f8 --- /dev/null +++ b/test/builds-sr-ht.exs @@ -0,0 +1,52 @@ +name = + case System.cmd("git", ~w(describe --all --long --dirty --broken --always)) do + {name, 0} -> String.trim(name) + _ -> "cannot-git-describe" + end + +repo = System.get_env("TEST_REPO") || "https://git.sr.ht/~href/gen_magic" + +IO.puts("Using repository: #{repo}") + +token = System.get_env("SR_HT_TOKEN") + +unless token do + IO.puts(""" + sr.ht token not defined (SR_HT_TOKEN)\n\n + Get one at https://meta.sr.ht/oauth/personal-token\n + Define one by setting the SR_HT_TOKEN environment variable + """) +else + Application.ensure_all_started(:ssl) + Application.ensure_all_started(:inets) + + File.ls!(".builds") + |> Enum.filter(fn file -> Path.extname(file) == ".yaml" end) + |> Enum.each(fn file -> + file = Path.join(".builds", file) + build = Path.basename(file, ".yaml") + + build = + %{ + "manifest" => File.read!(file), + "note" => "gen_magic/#{name} #{build}", + "tags" => ["gen_magic"] + } + |> Jason.encode!() + + case :httpc.request( + :post, + {'https://builds.sr.ht/api/jobs', [{'authorization', 'token ' ++ to_charlist(token)}], + 'application/json', build}, + [], + [] + ) do + {:ok, {{_http_v, 200, 'OK'}, _headers, body}} -> + resp = Jason.decode!(body) + IO.puts("#{resp["status"]} job #{resp["note"]}, id: #{resp["id"]}") + + error -> + IO.puts("Failed to enqueue job #{inspect(error)}") + end + end) +end diff --git a/test/majic/plug_test.exs b/test/majic/plug_test.exs index a850cb5..63c2dd9 100644 --- a/test/majic/plug_test.exs +++ b/test/majic/plug_test.exs @@ -5,12 +5,15 @@ defmodule Majic.PlugTest do defmodule TestRouter do use Plug.Router - plug :match - plug :dispatch - plug Plug.Parsers, + plug(:match) + plug(:dispatch) + + plug(Plug.Parsers, parsers: [:urlencoded, :multipart], pass: ["*/*"] - #plug Majic.Plug, once: true + ) + + # plug Majic.Plug, once: true post "/" do send_resp(conn, 200, "Ok") @@ -44,13 +47,14 @@ defmodule Majic.PlugTest do ------w58EW1cEpjzydSCq--\r """ - orig_conn = conn(:post, "/", multipart) - |> put_req_header("content-type", "multipart/mixed; boundary=----w58EW1cEpjzydSCq") - |> TestRouter.call(@router_opts) + orig_conn = + conn(:post, "/", multipart) + |> put_req_header("content-type", "multipart/mixed; boundary=----w58EW1cEpjzydSCq") + |> TestRouter.call(@router_opts) - plug = Majic.Plug.init([once: true]) - plug_no_ext = Majic.Plug.init([once: true, fix_extension: false]) - plug_append_ext = Majic.Plug.init([once: true, fix_extension: true, append_extension: true]) + plug = Majic.Plug.init(once: true) + plug_no_ext = Majic.Plug.init(once: true, fix_extension: false) + plug_append_ext = Majic.Plug.init(once: true, fix_extension: true, append_extension: true) conn = Majic.Plug.call(orig_conn, plug) conn_no_ext = Majic.Plug.call(orig_conn, plug_no_ext) @@ -58,15 +62,23 @@ defmodule Majic.PlugTest do assert conn.state == :sent assert conn.status == 200 - refute get_in(conn.body_params, ["form", "makefile"]).content_type == get_in(conn.params, ["form", "makefile"]).content_type + + refute get_in(conn.body_params, ["form", "makefile"]).content_type == + get_in(conn.params, ["form", "makefile"]).content_type + assert get_in(conn.params, ["form", "makefile"]).content_type == "text/x-makefile" - refute get_in(conn.body_params, ["form", "make", "file"]).content_type == get_in(conn.params, ["form", "make", "file"]).content_type + + refute get_in(conn.body_params, ["form", "make", "file"]).content_type == + get_in(conn.params, ["form", "make", "file"]).content_type + assert get_in(conn.params, ["form", "make", "file"]).content_type == "text/x-makefile" - refute get_in(conn.body_params, ["cat"]).content_type == get_in(conn.params, ["cat"]).content_type + + refute get_in(conn.body_params, ["cat"]).content_type == + get_in(conn.params, ["cat"]).content_type + assert get_in(conn.params, ["cat"]).content_type == "image/webp" assert get_in(conn.params, ["cat"]).filename == "cute-cat.webp" assert get_in(conn_no_ext.params, ["cat"]).filename == "cute-cat.jpg" assert get_in(conn_append_ext.params, ["cat"]).filename == "cute-cat.jpg.webp" end - end