Skip to content

Commit

Permalink
URI post and comment fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdunkerley committed Sep 29, 2023
1 parent 4b4a134 commit dd20a74
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 68 deletions.
5 changes: 2 additions & 3 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Data.enso
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,9 @@ list_directory directory name_filter=Nothing recursive=False =
- method: The HTTP method to use. Must be one of `HTTP_Method.Get`,
`HTTP_Method.Head`, `HTTP_Method.Delete`, `HTTP_Method.Options`.
Defaults to `HTTP_Method.Get`.

- headers: The headers to send with the request. Defaults to an empty vector.
- try_auto_parse: If successful should the body be attempted to be parsed to
an Enso native object.
- try_auto_parse_response: If successful should the body be attempted to be
parsed to an Enso native object.

> Example
Read from an HTTP endpoint.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ Text.to_uri self = URI.parse self
represent a successful response.

Arguments:
- method: The HTTP method to use. Defaults to `GET`.
- method: The HTTP method to use. Must be one of `HTTP_Method.Get`,
`HTTP_Method.Head`, `HTTP_Method.Delete`, `HTTP_Method.Options`.
Defaults to `HTTP_Method.Get`.
- headers: The headers to send with the request. Defaults to an empty vector.
- try_auto_parse: If successful should the body be attempted to be parsed to
an Enso native object.
- try_auto_parse_response: If successful should the body be attempted to be
parsed to an Enso native object.
URI.fetch : HTTP_Method -> Vector (Header | Pair Text Text) -> Boolean -> Any
URI.fetch self (method:HTTP_Method=HTTP_Method.Get) headers=[] try_auto_parse=True =
Data.fetch self method headers try_auto_parse
URI.fetch self (method:HTTP_Method=HTTP_Method.Get) headers=[] try_auto_parse_response=True =
Data.fetch self method headers try_auto_parse_response

## ALIAS upload, http post
GROUP Input
Expand All @@ -47,13 +49,12 @@ URI.fetch self (method:HTTP_Method=HTTP_Method.Get) headers=[] try_auto_parse=Tr
status code does not represent a successful response.

Arguments:
- uri: The URI to fetch.
- body: The data to write. See `Supported Body Types` below.
- method: The HTTP method to use. Must be one of `HTTP_Method.Post`,
`HTTP_Method.Put`, `HTTP_Method.Patch`. Defaults to `HTTP_Method.Post`.
- headers: The headers to send with the request. Defaults to an empty vector.
- try_auto_parse: If successful should the body be attempted to be parsed to
an Enso native object.
- try_auto_parse_response: If successful should the body be attempted to be
parsed to an Enso native object.

! Specifying Content Types

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import project.Errors.Invalid_JSON_Format
import project.Internal.Parse_To_Table
import project.Internal.Widget_Helpers

## Expand a column of objects to a set of columns
## GROUP Standard.Base.Conversions
Expand a column of objects to a set of columns
@column Widget_Helpers.make_column_name_selector
Table.expand_column self column prefix=Nothing fields=Nothing =
Table.expand_column self column fields=Nothing prefix=Nothing =
column_object = self.at column
expanded = Table.from_objects column_object.to_vector fields

Expand Down
70 changes: 15 additions & 55 deletions test/Tests/src/Network/Http_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -88,37 +88,27 @@ spec =
}
response . should_equal expected_response

Test.specify "Can perform a GET with a JSON response using URI.fetch" <|
response = url_get.to_uri.fetch
expected_response = Json.parse <| '''
{
"headers": {
"User-Agent": "Java-http-client/17.0.7",
"Content-Type": "text/plain",
"Content-Length": "0"
},
"origin": "127.0.0.1",
"url": "",
"method": "GET",
"args": {}
}
response . should_equal expected_response
uri_response = url_get.to_uri.fetch
uri_response . should_equal expected_response

Test.specify "Can perform a HEAD" <|
response = Data.fetch url_head method=HTTP_Method.Head
response.code.code . should_equal 200
response.decode_as_text . should_equal ''

Test.specify "Can perform a HEAD via URI.fetch" <|
response = url_head.to_uri.fetch method=HTTP_Method.Head
response.code.code . should_equal 200
response.decode_as_text . should_equal ''
uri_response = url_head.to_uri.fetch method=HTTP_Method.Head
uri_response.code.code . should_equal 200
uri_response.decode_as_text . should_equal ''

Test.specify "Can perform an OPTIONS" <|
response = Data.fetch url_options method=HTTP_Method.Options
response.code.code . should_equal 200
response.decode_as_text . should_equal ''

uri_response = url_head.to_uri.fetch method=HTTP_Method.Options
uri_response.code.code . should_equal 200
uri_response.decode_as_text . should_equal ''

Test.specify "Can perform auto-parse" <|
response = Data.fetch url_get
response.at "headers" . at "Content-Length" . should_equal "0"
Expand All @@ -139,21 +129,9 @@ spec =
}
response.decode_as_json . should_equal expected_response

Test.specify "Can skip auto-parse with URI.fetch" <|
response = url_get.to_uri.fetch try_auto_parse_response=False
response.code.code . should_equal 200
expected_response = Json.parse <| '''
{
"headers": {
"User-Agent": "Java-http-client/17.0.7",
"Content-Length": "0"
},
"origin": "127.0.0.1",
"url": "",
"method": "GET",
"args": {}
}
response.decode_as_json . should_equal expected_response
uri_response = url_get.to_uri.fetch try_auto_parse_response=False
uri_response.code.code . should_equal 200
uri_response.decode_as_json . should_equal expected_response

Test.specify "Can still perform request when output context is disabled" <|
run_with_and_without_output <|
Expand Down Expand Up @@ -202,26 +180,8 @@ spec =
}
response . should_equal expected_response

Test.specify "Can perform a Request_Body.Text POST via URI.post" <|
response = url_post.to_uri.post (Request_Body.Text "hello world")
expected_response = Json.parse <| '''
{
"headers": {
"Content-Encoding": "UTF-8",
"User-Agent": "Java-http-client/17.0.7",
"Content-Type": "text/plain",
"Content-Length": "11"
},
"origin": "127.0.0.1",
"url": "",
"method": "POST",
"form": null,
"files": null,
"data": "hello world",
"json": null,
"args": {}
}
response . should_equal expected_response
url_response = url_post.to_uri.post (Request_Body.Text "hello world")
url_response . should_equal expected_response

Test.specify "Can perform a Request_Body.Json JSON POST" <|
json = Json.parse '{"a": "asdf", "b": 123}'
Expand Down Expand Up @@ -344,7 +304,7 @@ spec =
response . should_equal expected_response

Test.specify "Can perform a File POST" <|
test_file = enso_project.data / "sample.txt"
test_file = enso_pr ject.data / "sample.txt"
response = Data.post url_post (Request_Body.Binary test_file)
response.at "headers" . at "Content-Type" . should_equal "application/octet-stream"
expected_text = test_file.read_text
Expand Down

0 comments on commit dd20a74

Please sign in to comment.