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

HTTP Library Updates #1722

Merged
merged 4 commits into from
May 4, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
19 changes: 12 additions & 7 deletions distribution/std-lib/Standard/src/Base/Network/Http.enso
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ type Http
example_post_json =
json = Json.parse '{"key":"val"}'
Examples.http_client.post_json "http://httpbin.org/post" json
post_json : (Text | Uri) -> Json -> Vector.Vector -> Response ! Request_Error
post_json : (Text | Uri) -> (Text | Json) -> Vector.Vector -> Response ! Request_Error
post_json uri body_json (headers = []) =
new_headers = [Header.application_json]
req = Request.post uri (Request_Body.Json body_json) headers . with_headers new_headers
Expand Down Expand Up @@ -443,7 +443,7 @@ type Http
example_post_json =
json = Json.parse '{"key":"val"}'
Examples.http_client.put_json "http://httpbin.org/post" json
put_json : (Text | Uri) -> Json -> Vector.Vector -> Response ! Request_Error
put_json : (Text | Uri) -> (Text | Json) -> Vector.Vector -> Response ! Request_Error
put_json uri body_json (headers = []) =
new_headers = [Header.application_json]
req = Request.put uri (Request_Body.Json body_json) headers . with_headers new_headers
Expand Down Expand Up @@ -562,7 +562,8 @@ type Http
Pair req (body_publishers.ofString text)
Request_Body.Json json ->
builder.header Header.application_json.name Header.application_json.value
Pair req (body_publishers.ofString json.to_text)
json_body = if json.is_a Text then json else json.to_text
Pair req (body_publishers.ofString json_body)
Request_Body.Form form ->
add_multipart form =
body_builder = Http_Utils.multipart_body_builder
Expand Down Expand Up @@ -603,7 +604,7 @@ type Http
Response.response (this.internal_http_client.send http_request body_handler)
response.catch e-> case e of
Polyglot_Error err ->
Error.throw (Request_Error err.getMessage)
Error.throw (Request_Error err.getClass.getSimpleName err.getMessage)
_ ->
Error.throw e

Expand Down Expand Up @@ -646,11 +647,15 @@ type Http
An error when sending an Http request.

Arguments:
- error_type: The type of the error.
- message: The message for the error.
type Request_Error message
type Request_Error error_type message

## UNSTABLE

Convert a request error to a human-readable form.
Request_Error.to_display_text = "Error when sending request: " + this.message

Request_Error.to_display_text =
description_text = case this.message of
Nothing -> ""
_ -> " " + this.message
this.error_type + " error when sending request." + description_text
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ type Request
import Standard.Base.Network.Uri

example_with_json =
Request.post (Uri.parse "http://example.com") Request_Body.Empty |> .with_body "{ "a": "b" }"
with_json : Text -> Request
Request.post (Uri.parse "http://example.com") Request_Body.Empty |> .with_json '{ "a": "b" }'
with_json : (Text | Json) -> Request
with_json json_body =
new_body = Request_Body.Json json_body
Request this.method this.uri this.headers new_body . with_headers [Header.application_json]
Expand Down
2 changes: 1 addition & 1 deletion distribution/std-lib/Standard/src/Base/Network/Uri.enso
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ type Uri

example_to_json = Examples.uri.to_json
to_json : Json.String
to_json : Json.String this.to_text
to_json = Json.String this.to_text

## Check if this URI is equal to another URI.

Expand Down
71 changes: 71 additions & 0 deletions test/Tests/src/Network/Http_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Standard.Base.Data.Time.Duration
import Standard.Base.Network.Http
import Standard.Base.Network.Http.Form
import Standard.Base.Network.Http.Header
import Standard.Base.Network.Http.Method
import Standard.Base.Network.Http.Request
import Standard.Base.Network.Http.Request.Body as Request_Body
import Standard.Base.Network.Http.Status_Code
Expand Down Expand Up @@ -37,6 +38,7 @@ spec =
http.version.should_equal version_setting
Test.specify "should throw error when requesting invalid Uri" <|
Http.new.get "not a uri" . should_fail_with Uri.Syntax_Error

Test.specify "should send Get request" <|
expected_response = Json.parse <| '''
{
Expand Down Expand Up @@ -65,6 +67,7 @@ spec =
res = Http.get "http://localhost:8080/get"
res.code.should_equal Status_Code.ok
res.body.to_json.should_equal expected_response

Test.specify "should fetch the body of a Get request" <|
expected_response = Json.parse <| '''
{
Expand All @@ -80,10 +83,12 @@ spec =
res.to_json.should_equal expected_response
Test.specify "should return error if the fetch method fails" <|
Http.fetch "http://undefined_host" . should_fail_with Http.Request_Error

Test.specify "should send Head request" <|
res = Http.new.head "http://localhost:8080/get"
res.code.should_equal Status_Code.ok
res.body.to_text.should_equal ''

Test.specify "should Post empty body" <|
expected_response = Json.parse <| '''
{
Expand Down Expand Up @@ -258,3 +263,69 @@ spec =
res = Http.new.post "http://localhost:8080/post" body_bytes
res.code.should_equal Status_Code.ok
res.body.to_json.should_equal expected_response

Test.specify "should create and send Get request" <|
expected_response = Json.parse <| '''
{
"headers": {
"Content-Length": "0",
"User-Agent": "Java-http-client/11.0.10"
},
"origin": "127.0.0.1",
"url": "",
"args": {}
}
req = Request.new Method.Get "http://localhost:8080/get"
res = Http.new.request req
res.code.should_equal Status_Code.ok
res.body.to_json.should_equal expected_response
Test.specify "should create and send Post request with json body" <|
expected_response = Json.parse <| '''
{
"headers": {
"Content-Length": "13",
"Content-Type": "application/json",
"User-Agent": "Java-http-client/11.0.10"
},
"origin": "127.0.0.1",
"url": "",
"args": {},
"data": "{\\"key\\":\\"val\\"}",
"files": null,
"form": null,
"json": {
"key": "val"
}
}
json_body = Json.parse <| '''
{ "key": "val" }
req = Request.new Method.Post "http://localhost:8080/post"
req_with_body = req.with_json json_body
res = Http.new.request req_with_body
res.code.should_equal Status_Code.ok
res.body.to_json.should_equal expected_response
Test.specify "should create and send Post request with json text" <|
expected_response = Json.parse <| '''
{
"headers": {
"Content-Length": "16",
"Content-Type": "application/json",
"User-Agent": "Java-http-client/11.0.10"
},
"origin": "127.0.0.1",
"url": "",
"args": {},
"data": "{ \\"key\\": \\"val\\" }",
"files": null,
"form": null,
"json": {
"key": "val"
}
}
json_text = '''
{ "key": "val" }
req = Request.new Method.Post "http://localhost:8080/post"
req_with_body = req.with_json json_text
res = Http.new.request req_with_body
res.code.should_equal Status_Code.ok
res.body.to_json.should_equal expected_response