Page MenuHomePhorge

No OneTemporary

Size
2 KB
Referenced Files
None
Subscribers
None
diff --git a/test/operation2_test.exs b/test/operation2_test.exs
new file mode 100644
index 0000000..2644a8a
--- /dev/null
+++ b/test/operation2_test.exs
@@ -0,0 +1,90 @@
+defmodule OpenApiSpex.Operation2Test do
+ use ExUnit.Case
+ alias OpenApiSpex.{Operation, Operation2, Schema}
+
+ defmodule SchemaFixtures do
+ @user %Schema{
+ type: :object,
+ properties: %{
+ user: %Schema{
+ type: :object,
+ properties: %{
+ email: %Schema{type: :string}
+ }
+ }
+ }
+ }
+ @schemas %{"User" => @user}
+
+ def user, do: @user
+ def schemas, do: @schemas
+ end
+
+ defmodule OperationFixtures do
+ @create_user %Operation{
+ operationId: "UserController.create",
+ requestBody:
+ Operation.request_body("request body", "application/json", SchemaFixtures.user(),
+ required: true
+ ),
+ responses: %{
+ 200 => Operation.response("User", "application/json", SchemaFixtures.user())
+ }
+ }
+
+ def create_user, do: @create_user
+ end
+
+ defmodule SpecModule do
+ def spec do
+ paths = %{
+ "/users" => %{
+ "post" => OperationFixtures.create_user()
+ }
+ }
+
+ %OpenApiSpex.OpenApi{
+ info: nil,
+ paths: paths,
+ components: %{
+ schemas: SchemaFixtures.schemas()
+ }
+ }
+ end
+ end
+
+ defmodule RenderError do
+ def init(_) do
+ nil
+ end
+
+ def call(_conn, _errors) do
+ raise "should not have errors"
+ end
+ end
+
+ describe "cast/4" do
+ test "cast request body" do
+ conn = create_conn(%{"user" => %{"email" => "foo@bar.com"}})
+
+ assert {:ok, value} =
+ Operation2.cast(
+ OperationFixtures.create_user(),
+ conn,
+ "application/json",
+ SchemaFixtures.schemas()
+ )
+
+ assert %Plug.Conn{} = value
+ end
+
+ # TODO Verify query params are casted
+
+ defp create_conn(body_params) do
+ :post
+ |> Plug.Test.conn("/api/users")
+ |> Plug.Conn.put_req_header("content-type", "application/json")
+ |> Map.put(:body_params, body_params)
+ end
+ end
+end

File Metadata

Mime Type
text/x-diff
Expires
Sat, Nov 30, 3:58 PM (1 d, 20 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41495
Default Alt Text
(2 KB)

Event Timeline