Skip to content

Commit

Permalink
Use a simple Phoenix app to serve schema files in tests
Browse files Browse the repository at this point in the history
Apparently httpd is not available in recent OTP versions anymore.
  • Loading branch information
jonasschmidt committed Oct 17, 2023
1 parent ee3f333 commit 9627cc1
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ If you have remote schemata that need to be fetched at runtime, you have to regi
```elixir
config :ex_json_schema,
:remote_schema_resolver,
fn url -> HTTPoison.get!(url).body |> Poison.decode! end
fn url -> HTTPoison.get!(url).body |> Jason.decode! end
```

Alternatively, you can specify a module and function name for situations where using anonymous functions is not possible (i.e. working with Erlang releases):
Expand Down
9 changes: 7 additions & 2 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ defmodule CustomFormatValidator do
end

config :ex_json_schema,
decode_json: fn json -> Poison.decode(json) end,
remote_schema_resolver: fn url -> HTTPoison.get!(url).body |> Poison.decode!() end,
decode_json: fn json -> Jason.decode(json) end,
remote_schema_resolver: fn url -> HTTPoison.get!(url).body |> Jason.decode!() end,
custom_format_validator: {CustomFormatValidator, :validate}

config :ex_json_schema, SamplePhoenix.Endpoint,
http: [ip: {127, 0, 0, 1}, port: 1234],
server: true,
secret_key_base: String.duplicate("a", 64)
4 changes: 3 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ defmodule ExJsonSchema.Mixfile do
{:excoveralls, "~> 0.14", only: :test},
{:httpoison, "~> 1.8", only: :test},
{:mix_test_watch, "~> 0.7", only: [:dev, :test]},
{:poison, "~> 5.0", only: :test}
{:jason, "~> 1.4", only: :test},
{:plug_cowboy, "~> 2.5", only: :test},
{:phoenix, "~> 1.6", only: :test}
]
end

Expand Down
10 changes: 5 additions & 5 deletions test/ex_json_schema/schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,19 @@ defmodule ExJsonSchema.SchemaTest do
test "fetching a ref schema with a path" do
schema =
resolve(%{
"properties" => %{"foo" => %{"$ref" => "http://localhost:8000/subschema.json#/foo"}}
"properties" => %{"foo" => %{"$ref" => "http://localhost:1234/subschema.json#/foo"}}
})

assert get_fragment!(schema, "#/properties/foo") == %{
"$ref" => ["http://localhost:8000/subschema.json", "foo"]
"$ref" => ["http://localhost:1234/subschema.json", "foo"]
}
end

test "fetching a ref schema with a URL" do
schema = resolve(%{"$ref" => "http://localhost:8000/subschema.json#/foo"})
schema = resolve(%{"$ref" => "http://localhost:1234/subschema.json#/foo"})

assert get_fragment!(schema, "http://localhost:8000/subschema.json#/foo") == %{
"$ref" => ["http://localhost:8000/subsubschema.json", "foo"]
assert get_fragment!(schema, "http://localhost:1234/subschema.json#/foo") == %{
"$ref" => ["http://localhost:1234/subsubschema.json", "foo"]
}
end
end
4 changes: 2 additions & 2 deletions test/ex_json_schema/validator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule ExJsonSchema.ValidatorTest do

@schema_with_ref Schema.resolve(%{
"properties" => %{
"foo" => %{"$ref" => "http://localhost:8000/subschema.json#/foo"}
"foo" => %{"$ref" => "http://localhost:1234/subschema.json#/foo"}
}
})

Expand Down Expand Up @@ -66,7 +66,7 @@ defmodule ExJsonSchema.ValidatorTest do

test "validation errors with a remote reference within a remote reference" do
assert_validation_errors(
%{"$ref" => "http://localhost:8000/subschema.json#/foo"},
%{"$ref" => "http://localhost:1234/subschema.json#/foo"},
"foo",
[{"Type mismatch. Expected Integer but got String.", "#"}],
[%Error{error: %Error.Type{expected: ["integer"], actual: "string"}, path: "#"}]
Expand Down
34 changes: 34 additions & 0 deletions test/support/remote_schema_server.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
defmodule SamplePhoenix.SampleController do
use Phoenix.Controller

def show(conn, %{"path" => path}) do
test_path = Path.join(__DIR__, "..") |> Path.expand()

json =
case File.read(Path.join([test_path, "support/schemata"] ++ path)) do
{:ok, json} -> json
_ -> File.read!(Path.join([test_path, "JSON-Schema-Test-Suite/remotes"] ++ path))
end

conn |> put_resp_content_type("application/json") |> send_resp(200, json)
end
end

defmodule Router do
use Phoenix.Router

pipeline :api do
plug(:accepts, ["json"])
end

scope "/", SamplePhoenix, log: false do
pipe_through(:api)

get("/*path", SampleController, :show)
end
end

defmodule SamplePhoenix.Endpoint do
use Phoenix.Endpoint, otp_app: :ex_json_schema
plug(Router)
end
2 changes: 1 addition & 1 deletion test/support/schemata/subschema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"foo": {
"$ref": "http://localhost:8000/subsubschema.json#/foo"
"$ref": "http://localhost:1234/subsubschema.json#/foo"
}
}
2 changes: 1 addition & 1 deletion test/support/test_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ defmodule ExJsonSchema.Test.Support.TestHelpers do
schema_tests_path
|> Path.join(name <> ".json")
|> File.read!()
|> Poison.decode!()
|> Jason.decode!()
end
end
16 changes: 1 addition & 15 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
ExUnit.start()
HTTPoison.start()

{:ok, _} =
:inets.start(:httpd,
server_name: 'test 1',
document_root: './test/JSON-Schema-Test-Suite/remotes',
server_root: '.',
port: 1234
)

{:ok, _} =
:inets.start(:httpd,
server_name: 'test 2',
document_root: './test/support/schemata',
server_root: '.',
port: 8000
)
{:ok, _} = Supervisor.start_link([SamplePhoenix.Endpoint], strategy: :one_for_one)

0 comments on commit 9627cc1

Please sign in to comment.