Skip to content

Commit 65b7bcd

Browse files
committed
move plug helpers
1 parent 7ed22c1 commit 65b7bcd

2 files changed

Lines changed: 95 additions & 108 deletions

File tree

test/plug_test.exs

Lines changed: 7 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ defmodule OCI.PlugTest do
66
use ExUnit.Case, async: true
77
import Plug.Conn
88
import Plug.Test
9+
import OCI.PlugTest.Helpers
910

1011
alias OCI.Registry
1112

@@ -27,6 +28,12 @@ defmodule OCI.PlugTest do
2728
%{conn: conn}
2829
end
2930

31+
defp plug_opts() do
32+
{:ok, tmp_path} = Temp.path()
33+
registry = Registry.init(storage: OCI.Storage.Local.init(path: tmp_path))
34+
OCI.Plug.init(registry: registry)
35+
end
36+
3037
describe "GET /" do
3138
test "returns 200 for base endpoint", %{conn: conn} do
3239
conn = conn |> get("/")
@@ -56,112 +63,4 @@ defmodule OCI.PlugTest do
5663
assert String.starts_with?(location, "/v2/big-org/big-team/big-project/blobs/uploads/")
5764
end
5865
end
59-
60-
defp get(conn, path, query_params \\ nil) do
61-
conn
62-
|> Plug.Adapters.Test.Conn.conn(:get, path, query_params)
63-
|> OCI.Plug.call(conn.assigns.oci_opts)
64-
end
65-
66-
defp post(conn, path, body \\ nil) do
67-
conn
68-
|> Plug.Adapters.Test.Conn.conn(:post, path, body)
69-
|> OCI.Plug.call(conn.assigns.oci_opts)
70-
end
71-
72-
defp put(conn, path, body \\ nil) do
73-
conn
74-
|> Plug.Adapters.Test.Conn.conn(:put, path, body)
75-
|> OCI.Plug.call(conn.assigns.oci_opts)
76-
end
77-
78-
defp head(conn, path) do
79-
conn
80-
|> Plug.Adapters.Test.Conn.conn(:head, path, nil)
81-
|> OCI.Plug.call(conn.assigns.oci_opts)
82-
end
83-
84-
defp delete(conn, path) do
85-
conn
86-
|> Plug.Adapters.Test.Conn.conn(:delete, path, nil)
87-
|> OCI.Plug.call(conn.assigns.oci_opts)
88-
end
89-
90-
defp patch(conn, path, body) do
91-
conn
92-
|> Plug.Adapters.Test.Conn.conn(:patch, path, body)
93-
|> OCI.Plug.call(conn.assigns.oci_opts)
94-
end
95-
96-
defp plug_opts() do
97-
{:ok, tmp_path} = Temp.path()
98-
registry = Registry.init(storage: OCI.Storage.Local.init(path: tmp_path))
99-
OCI.Plug.init(registry: registry)
100-
end
101-
102-
defp digest(data) do
103-
digest = OCI.Registry.sha256(data)
104-
"sha256:#{digest}"
105-
end
106-
107-
defp initiate_blob_upload(%Plug.Conn{} = conn, repo) do
108-
conn =
109-
conn
110-
|> post("/#{repo}/blobs/uploads/")
111-
112-
assert conn.status == 202
113-
[location] = get_resp_header(conn, "location")
114-
uuid = location |> String.split("/") |> List.last()
115-
{conn, uuid}
116-
end
117-
118-
# Helper function to upload a chunk to a blob upload
119-
defp upload_chunk(%Plug.Conn{} = conn, repo, uuid, chunk, start_range \\ 0) do
120-
end_range = start_range + String.length(chunk) - 1
121-
122-
conn =
123-
conn
124-
|> put_req_header("content-type", "application/octet-stream")
125-
|> put_req_header("content-range", "#{start_range}-#{end_range}")
126-
|> put_req_header("content-length", "#{String.length(chunk)}")
127-
|> patch("/#{repo}/blobs/uploads/#{uuid}", chunk)
128-
129-
assert conn.status == 202
130-
assert [location] = get_resp_header(conn, "location")
131-
assert String.starts_with?(location, "/v2/#{repo}/blobs/uploads/")
132-
assert [range] = get_resp_header(conn, "range")
133-
assert range == "#{start_range}-#{end_range}"
134-
{conn, end_range}
135-
end
136-
137-
# Helper function to complete a blob upload
138-
defp complete_blob_upload(%Plug.Conn{} = conn, repo, uuid, digest, final_chunk \\ "") do
139-
conn =
140-
conn
141-
|> put_req_header("content-type", "application/octet-stream")
142-
|> put_req_header("content-length", "#{String.length(final_chunk)}")
143-
|> put("/#{repo}/blobs/uploads/#{uuid}?digest=#{digest}", final_chunk)
144-
145-
assert conn.status == 201
146-
assert [location] = get_resp_header(conn, "location")
147-
assert String.starts_with?(location, "/v2/#{repo}/blobs/")
148-
assert String.ends_with?(location, digest)
149-
conn
150-
end
151-
152-
defp basic_auth(conn, username, password) do
153-
put_req_header(conn, "authorization", "Basic #{Base.encode64("#{username}:#{password}")}")
154-
end
155-
156-
defp override_registry_setting(conn, setting, value) do
157-
registry = %{conn.assigns.oci_opts.registry | setting => value}
158-
159-
%{
160-
conn
161-
| assigns: %{
162-
conn.assigns
163-
| oci_opts: %{conn.assigns.oci_opts | registry: registry}
164-
}
165-
}
166-
end
16766
end

test/support/plug_test_helpers.ex

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
defmodule OCI.PlugTest.Helpers do
2+
@moduledoc """
3+
Helpers for the OCI.Plug test suite.
4+
5+
This module is used to test the OCI.Plug module.
6+
It provides a set of functions that can be used to make requests to the OCI.Plug module.
7+
8+
The functions are named after the HTTP methods they correspond to.
9+
"""
10+
11+
import Plug.Conn
12+
13+
def get(conn, path, query_params \\ nil) do
14+
conn
15+
|> Plug.Adapters.Test.Conn.conn(:get, path, query_params)
16+
|> OCI.Plug.call(conn.assigns.oci_opts)
17+
end
18+
19+
def post(conn, path, body \\ nil) do
20+
conn
21+
|> Plug.Adapters.Test.Conn.conn(:post, path, body)
22+
|> OCI.Plug.call(conn.assigns.oci_opts)
23+
end
24+
25+
def put(conn, path, body \\ nil) do
26+
conn
27+
|> Plug.Adapters.Test.Conn.conn(:put, path, body)
28+
|> OCI.Plug.call(conn.assigns.oci_opts)
29+
end
30+
31+
def head(conn, path) do
32+
conn
33+
|> Plug.Adapters.Test.Conn.conn(:head, path, nil)
34+
|> OCI.Plug.call(conn.assigns.oci_opts)
35+
end
36+
37+
def delete(conn, path) do
38+
conn
39+
|> Plug.Adapters.Test.Conn.conn(:delete, path, nil)
40+
|> OCI.Plug.call(conn.assigns.oci_opts)
41+
end
42+
43+
def patch(conn, path, body) do
44+
conn
45+
|> Plug.Adapters.Test.Conn.conn(:patch, path, body)
46+
|> OCI.Plug.call(conn.assigns.oci_opts)
47+
end
48+
49+
def digest(data) do
50+
digest = OCI.Registry.sha256(data)
51+
"sha256:#{digest}"
52+
end
53+
54+
# Helper function to upload a chunk to a blob upload
55+
def upload_chunk(%Plug.Conn{} = conn, repo, uuid, chunk, start_range \\ 0) do
56+
end_range = start_range + byte_size(chunk) - 1
57+
58+
conn
59+
|> put_req_header("content-type", "application/octet-stream")
60+
|> put_req_header("content-range", "#{start_range}-#{end_range}")
61+
|> put_req_header("content-length", "#{byte_size(chunk)}")
62+
|> patch("/#{repo}/blobs/uploads/#{uuid}", chunk)
63+
end
64+
65+
# Helper function to complete a blob upload
66+
def complete_blob_upload(%Plug.Conn{} = conn, repo, uuid, digest, final_chunk \\ "") do
67+
conn
68+
|> put_req_header("content-type", "application/octet-stream")
69+
|> put_req_header("content-length", "#{byte_size(final_chunk)}")
70+
|> put("/#{repo}/blobs/uploads/#{uuid}?digest=#{digest}", final_chunk)
71+
end
72+
73+
def override_registry_setting(conn, setting, value) do
74+
registry = %{conn.assigns.oci_opts.registry | setting => value}
75+
76+
%{
77+
conn
78+
| assigns: %{
79+
conn.assigns
80+
| oci_opts: %{conn.assigns.oci_opts | registry: registry}
81+
}
82+
}
83+
end
84+
85+
def basic_auth(conn, username, password) do
86+
put_req_header(conn, "authorization", "Basic #{Base.encode64("#{username}:#{password}")}")
87+
end
88+
end

0 commit comments

Comments
 (0)