Page MenuHomePhorge

No OneTemporary

Size
9 KB
Referenced Files
None
Subscribers
None
diff --git a/.formatter.exs b/.formatter.exs
new file mode 100644
index 0000000..05293db
--- /dev/null
+++ b/.formatter.exs
@@ -0,0 +1,19 @@
+[
+ inputs: ["{mix,.formatter}.exs", "{config,lib,rel,test}/**/*.{ex,exs}"],
+ import_deps: [:plug],
+ locals_without_parens: [
+ transport: 2,
+ action_fallback: 1,
+ socket: 2,
+ render: 2,
+
+ # Distillery
+ set: 1,
+
+ # Phoenix.
+ pipe_through: 1,
+ head: 3,
+ get: 3,
+ post: 3
+ ]
+]
diff --git a/test/open_api_spex_test.exs b/test/open_api_spex_test.exs
deleted file mode 100644
index e2701bf..0000000
--- a/test/open_api_spex_test.exs
+++ /dev/null
@@ -1,67 +0,0 @@
-defmodule OpenApiSpexTest do
- use ExUnit.Case
- alias OpenApiSpexTest.ApiSpec
-
- describe "OpenApi" do
- test "compete" do
- spec = ApiSpec.spec()
- assert spec
- end
-
- test "Valid Request" do
- request_body = %{
- "user" => %{
- "id" => 123,
- "name" => "asdf",
- "email" => "foo@bar.com",
- "updated_at" => "2017-09-12T14:44:55Z"
- }
- }
-
- conn =
- :post
- |> Plug.Test.conn("/api/users", Poison.encode!(request_body))
- |> Plug.Conn.put_req_header("content-type", "application/json; charset=UTF-8")
- |> OpenApiSpexTest.Router.call([])
-
- assert conn.body_params == %OpenApiSpexTest.Schemas.UserRequest{
- user: %OpenApiSpexTest.Schemas.User{
- id: 123,
- name: "asdf",
- email: "foo@bar.com",
- updated_at: ~N[2017-09-12T14:44:55Z] |> DateTime.from_naive!("Etc/UTC")
- }
- }
-
- assert Poison.decode!(conn.resp_body) == %{
- "data" => %{
- "email" => "foo@bar.com",
- "id" => 1234,
- "inserted_at" => nil,
- "name" => "asdf",
- "updated_at" => "2017-09-12T14:44:55Z"
- }
- }
- end
-
- test "Invalid Request" do
- request_body = %{
- "user" => %{
- "id" => 123,
- "name" => "*1234",
- "email" => "foo@bar.com",
- "updated_at" => "2017-09-12T14:44:55Z"
- }
- }
-
- conn =
- :post
- |> Plug.Test.conn("/api/users", Poison.encode!(request_body))
- |> Plug.Conn.put_req_header("content-type", "application/json")
-
- conn = OpenApiSpexTest.Router.call(conn, [])
- assert conn.status == 422
- assert conn.resp_body == "#/user/name: Value \"*1234\" does not match pattern: [a-zA-Z][a-zA-Z0-9_]+"
- end
- end
-end
diff --git a/test/param_test.exs b/test/param_test.exs
deleted file mode 100644
index 2bcf8c5..0000000
--- a/test/param_test.exs
+++ /dev/null
@@ -1,79 +0,0 @@
-defmodule ParamTest do
- use ExUnit.Case
-
- describe "Param" do
- test "Valid Param" do
- conn =
- :get
- |> Plug.Test.conn("/api/users?validParam=true")
- |> OpenApiSpexTest.Router.call([])
-
- assert conn.status == 200
- end
-
- test "Invalid value" do
- conn =
- :get
- |> Plug.Test.conn("/api/users?validParam=123")
- |> OpenApiSpexTest.Router.call([])
-
- assert conn.status == 422
- end
-
- test "Invalid Param" do
- conn =
- :get
- |> Plug.Test.conn("/api/users?validParam=123&inValidParam=123&inValid2=hi")
- |> OpenApiSpexTest.Router.call([])
-
- assert conn.status == 422
- assert conn.resp_body == "Undefined query parameter: \"inValid2\""
- end
-
- test "with requestBody" do
- body = Poison.encode!(%{
- phone_number: "123-456-789",
- postal_address: "123 Lane St"
- })
-
- conn =
- :post
- |> Plug.Test.conn("/api/users/123/contact_info", body)
- |> Plug.Conn.put_req_header("content-type", "application/json")
- |> OpenApiSpexTest.Router.call([])
-
- assert conn.status == 200
- end
- end
-
- describe "Param with custom error handling" do
- test "Valid Param" do
- conn =
- :get
- |> Plug.Test.conn("/api/custom_error_users?validParam=true")
- |> OpenApiSpexTest.Router.call([])
-
- assert conn.status == 200
- end
-
- test "Invalid value" do
- conn =
- :get
- |> Plug.Test.conn("/api/custom_error_users?validParam=123")
- |> OpenApiSpexTest.Router.call([])
-
- assert conn.status == 400
- end
-
- test "Invalid Param" do
- conn =
- :get
- |> Plug.Test.conn("/api/custom_error_users?validParam=123&inValidParam=123&inValid2=hi")
- |> OpenApiSpexTest.Router.call([])
-
- assert conn.status == 400
- assert conn.resp_body == "Undefined query parameter: \"inValid2\""
- end
- end
-
-end
diff --git a/test/plug/cast_test.exs b/test/plug/cast_test.exs
new file mode 100644
index 0000000..4f37e47
--- /dev/null
+++ b/test/plug/cast_test.exs
@@ -0,0 +1,140 @@
+defmodule OpenApiSpex.Plug.CastTest do
+ use ExUnit.Case
+ alias OpenApiSpexTest.ApiSpec
+
+ describe "query params" do
+ test "Valid Param" do
+ conn =
+ :get
+ |> Plug.Test.conn("/api/users?validParam=true")
+ |> OpenApiSpexTest.Router.call([])
+
+ assert conn.status == 200
+ end
+
+ test "Invalid value" do
+ conn =
+ :get
+ |> Plug.Test.conn("/api/users?validParam=123")
+ |> OpenApiSpexTest.Router.call([])
+
+ assert conn.status == 422
+ end
+
+ test "Invalid Param" do
+ conn =
+ :get
+ |> Plug.Test.conn("/api/users?validParam=123&inValidParam=123&inValid2=hi")
+ |> OpenApiSpexTest.Router.call([])
+
+ assert conn.status == 422
+ assert conn.resp_body == "Undefined query parameter: \"inValid2\""
+ end
+
+ test "with requestBody" do
+ body =
+ Poison.encode!(%{
+ phone_number: "123-456-789",
+ postal_address: "123 Lane St"
+ })
+
+ conn =
+ :post
+ |> Plug.Test.conn("/api/users/123/contact_info", body)
+ |> Plug.Conn.put_req_header("content-type", "application/json")
+ |> OpenApiSpexTest.Router.call([])
+
+ assert conn.status == 200
+ end
+ end
+
+ describe "Param with custom error handling" do
+ test "Valid Param" do
+ conn =
+ :get
+ |> Plug.Test.conn("/api/custom_error_users?validParam=true")
+ |> OpenApiSpexTest.Router.call([])
+
+ assert conn.status == 200
+ end
+
+ test "Invalid value" do
+ conn =
+ :get
+ |> Plug.Test.conn("/api/custom_error_users?validParam=123")
+ |> OpenApiSpexTest.Router.call([])
+
+ assert conn.status == 400
+ end
+
+ test "Invalid Param" do
+ conn =
+ :get
+ |> Plug.Test.conn("/api/custom_error_users?validParam=123&inValidParam=123&inValid2=hi")
+ |> OpenApiSpexTest.Router.call([])
+
+ assert conn.status == 400
+ assert conn.resp_body == "Undefined query parameter: \"inValid2\""
+ end
+ end
+
+ describe "body params" do
+ test "Valid Request" do
+ request_body = %{
+ "user" => %{
+ "id" => 123,
+ "name" => "asdf",
+ "email" => "foo@bar.com",
+ "updated_at" => "2017-09-12T14:44:55Z"
+ }
+ }
+
+ conn =
+ :post
+ |> Plug.Test.conn("/api/users", Poison.encode!(request_body))
+ |> Plug.Conn.put_req_header("content-type", "application/json; charset=UTF-8")
+ |> OpenApiSpexTest.Router.call([])
+
+ assert conn.body_params == %OpenApiSpexTest.Schemas.UserRequest{
+ user: %OpenApiSpexTest.Schemas.User{
+ id: 123,
+ name: "asdf",
+ email: "foo@bar.com",
+ updated_at: ~N[2017-09-12T14:44:55Z] |> DateTime.from_naive!("Etc/UTC")
+ }
+ }
+
+ assert Poison.decode!(conn.resp_body) == %{
+ "data" => %{
+ "email" => "foo@bar.com",
+ "id" => 1234,
+ "inserted_at" => nil,
+ "name" => "asdf",
+ "updated_at" => "2017-09-12T14:44:55Z"
+ }
+ }
+ end
+
+ test "Invalid Request" do
+ request_body = %{
+ "user" => %{
+ "id" => 123,
+ "name" => "*1234",
+ "email" => "foo@bar.com",
+ "updated_at" => "2017-09-12T14:44:55Z"
+ }
+ }
+
+ conn =
+ :post
+ |> Plug.Test.conn("/api/users", Poison.encode!(request_body))
+ |> Plug.Conn.put_req_header("content-type", "application/json")
+
+ conn = OpenApiSpexTest.Router.call(conn, [])
+ assert conn.status == 422
+
+ assert conn.resp_body ==
+ "#/user/name: Value \"*1234\" does not match pattern: [a-zA-Z][a-zA-Z0-9_]+"
+ end
+ end
+end
diff --git a/test/support/router.ex b/test/support/router.ex
index 6fe3658..7e29bba 100644
--- a/test/support/router.ex
+++ b/test/support/router.ex
@@ -1,23 +1,26 @@
defmodule OpenApiSpexTest.Router do
use Phoenix.Router
alias Plug.Parsers
alias OpenApiSpexTest.UserController
alias OpenApiSpexTest.CustomErrorUserController
alias OpenApiSpex.Plug.{PutApiSpec, RenderSpec}
pipeline :api do
plug :accepts, ["json"]
plug PutApiSpec, module: OpenApiSpexTest.ApiSpec
plug Parsers, parsers: [:json], pass: ["text/*"], json_decoder: Poison
end
scope "/api" do
pipe_through :api
resources "/users", UserController, only: [:create, :index, :show]
+
+ # Used by ParamsTest
resources "/custom_error_users", CustomErrorUserController, only: [:index]
+
get "/users/:id/payment_details", UserController, :payment_details
post "/users/:id/contact_info", UserController, :contact_info
post "/users/create_entity", UserController, :create_entity
get "/openapi", RenderSpec, []
end
end

File Metadata

Mime Type
text/x-diff
Expires
Fri, Nov 29, 10:29 PM (1 d, 16 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41315
Default Alt Text
(9 KB)

Event Timeline