Skip to content
This repository was archived by the owner on Nov 7, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions lib/open_api_spex/plug/redoc_ui.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
defmodule OpenApiSpex.Plug.RedocUI do
@moduledoc """
Module plug that serves RedocUI.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add code examples on how to use this? See OpenApiSpex.Plug.SwaggerUI for an example.

"""

@behaviour Plug

@html """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ReDoc</title>
<!-- needed for adaptive design -->
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet">

<!--
ReDoc doesn't change outer page styles
-->
<style>
body {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<script>
window.onload = function() {
const apiSpecUrl = new URL(window.location);
pathname = "<%= path %>";
function specUrl() { return apiSpecUrl + pathname; };
</script>
<h1>SpecUrl: <script>specUrl();</script></h1>
<redoc spec-url='http://petstore.swagger.io/v2/swagger.json'></redoc>
<script src="https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js"> </script>
</body>
</html>
"""

@impl Plug
def init(path: path), do: %{path: path}

@impl Plug
def call(conn, %{path: path}) do
csrf_token = Plug.CSRFProtection.get_csrf_token()
html = render(path, csrf_token)

conn
|> Plug.Conn.put_resp_content_type("text/html")
|> Plug.Conn.send_resp(200, html)
end

require EEx
EEx.function_from_string(:defp, :render, @html, [:path, :csrf_token])
end
16 changes: 16 additions & 0 deletions test/plug/redoc_ui_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule OpenApiSpec.Plug.RedocUITest do
use ExUnit.Case

alias OpenApiSpex.Plug.RedocUI

@opts RedocUI.init(path: "/ui")

test "renders csrf token" do
# token = Plug.CSRFProtection.get_csrf_token()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the plan for this test?


# conn = Plug.Test.conn(:get, "/ui")
# conn = RedocUI.call(conn, @opts)
# assert conn.resp_body =~ ~r[pathname.+?/ui]
# assert String.contains?(conn.resp_body, token)
end
end