Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional API test case request helper methods #312

Merged
merged 1 commit into from
Sep 18, 2023
Merged
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
12 changes: 6 additions & 6 deletions src/components/framework/spec/routing_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ struct RoutingTest < ATH::Spec::APITestCase
end

def test_head_request : Nil
response = self.request "HEAD", "/head"
response = self.head "/head"
response.status.should eq HTTP::Status::OK
response.body.should be_empty
response.headers["content-length"].should eq "6" # JSON encoding adds 2 extra `"` chars
end

def test_head_request_on_get_endpoint : Nil
response = self.request "HEAD", "/get-head"
response = self.head "/get-head"
response.status.should eq HTTP::Status::OK
response.body.should be_empty
response.headers["FOO"].should eq "BAR" # Actually runs the controller action code
Expand Down Expand Up @@ -89,7 +89,7 @@ struct RoutingTest < ATH::Spec::APITestCase
end

def test_custom_response_status_head : Nil
self.request "HEAD", "/custom-status"
self.head "/custom-status"

self.assert_response_has_status :accepted
end
Expand Down Expand Up @@ -119,7 +119,7 @@ struct RoutingTest < ATH::Spec::APITestCase
end

def test_macro_dsl_head : Nil
response = self.request "HEAD", "/macro"
response = self.head "/macro"
response.status.should eq HTTP::Status::OK
response.body.should be_empty
end
Expand Down Expand Up @@ -225,7 +225,7 @@ struct RoutingTest < ATH::Spec::APITestCase
end

def test_redirects_head_request_to_route_without_trailing_slash : Nil
self.request "HEAD", "/head/"
self.head "/head/"

self.assert_response_redirects "/head"
end
Expand All @@ -237,7 +237,7 @@ struct RoutingTest < ATH::Spec::APITestCase
end

def test_redirects_head_request_to_route_with_trailing_slash : Nil
self.request "HEAD", "/head-get"
self.head "/head-get"

self.assert_response_redirects "/head-get/"
end
Expand Down
26 changes: 23 additions & 3 deletions src/components/framework/src/spec/api_test_case.cr
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,31 @@ abstract struct Athena::Framework::Spec::APITestCase < ATH::Spec::WebTestCase
@client.as(ATH::Spec::HTTPBrowser).not_nil!
end

# Makes a `DELETE` request to the provided *path*, optionally with the provided *headers*.
def delete(path : String, headers : HTTP::Headers = HTTP::Headers.new) : HTTP::Server::Response
self.request "DELETE", path, headers: headers
end

# Makes a `GET` request to the provided *path*, optionally with the provided *headers*.
def get(path : String, headers : HTTP::Headers = HTTP::Headers.new) : HTTP::Server::Response
self.request "GET", path, headers: headers
end

# Makes a `HEAD` request to the provided *path*, optionally with the provided *headers*.
def head(path : String, headers : HTTP::Headers = HTTP::Headers.new) : HTTP::Server::Response
self.request "HEAD", path, headers: headers
end

# Makes a `LINK` request to the provided *path*, optionally with the provided *headers*.
def link(path : String, headers : HTTP::Headers = HTTP::Headers.new) : HTTP::Server::Response
self.request "LINK", path, headers: headers
end

# Makes a `PATCH` request to the provided *path*, optionally with the provided *headers*.
def patch(path : String, headers : HTTP::Headers = HTTP::Headers.new) : HTTP::Server::Response
self.request "PATCH", path, headers: headers
end

# Makes a `POST` request to the provided *path*, optionally with the provided *body* and *headers*.
def post(path : String, body : String | Bytes | IO | Nil = nil, headers : HTTP::Headers = HTTP::Headers.new) : HTTP::Server::Response
self.request "POST", path, body, headers
Expand All @@ -136,9 +156,9 @@ abstract struct Athena::Framework::Spec::APITestCase < ATH::Spec::WebTestCase
self.request "PUT", path, body, headers
end

# Makes a `DELETE` request to the provided *path*, optionally with the provided *headers*.
def delete(path : String, headers : HTTP::Headers = HTTP::Headers.new) : HTTP::Server::Response
self.request "DELETE", path, headers: headers
# Makes a `UNLINK` request to the provided *path*, optionally with the provided *body* and *headers*.
def unlink(path : String, body : String | Bytes | IO | Nil = nil, headers : HTTP::Headers = HTTP::Headers.new) : HTTP::Server::Response
self.request "UNLINK", path, body, headers
end

# See `AbstractBrowser#request`.
Expand Down