-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] add HTTP URL stringify and testes
- Loading branch information
1 parent
629baa5
commit 70db076
Showing
5 changed files
with
148 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
# idris2-url | ||
WIP…… | ||
|
||
A URL library for idris2, Based on [RFC 1738](https://www.rfc-wiki.org/wiki/RFC1738). | ||
|
||
# Feature | ||
- [x] General Sytax | ||
- [ ] HTTP URL parse and stringify | ||
- [x] HTTP URL parse and stringify | ||
- [ ] HTTP URL utils | ||
- [ ] FTP URL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,26 @@ | ||
module Network.URL.HTTP | ||
|
||
|
||
import public Network.URL.HTTP.Data | ||
|
||
import Data.List | ||
import Data.Fin | ||
|
||
public export | ||
insertParam : QueryParam -> HTTPURL -> HTTPURL | ||
insertParam p@(key, value) url = { query := newQuery } url | ||
where | ||
params : List QueryParam | ||
params = url.query | ||
|
||
hasIn : Maybe (Fin (Prelude.List.length $ url.query)) | ||
hasIn = findIndex (\(k,_) => k == key) params | ||
|
||
update : Fin (length (url .query)) -> List QueryParam -> List QueryParam | ||
update idx ps = case inBounds (cast idx) ps of | ||
(Yes _) => replaceAt (cast idx) p ps | ||
_ => ps | ||
|
||
newQuery : List QueryParam | ||
newQuery = case hasIn of | ||
Nothing => snoc params p | ||
(Just idx) => update idx params |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,36 @@ | ||
module Network.URL.HTTP.Data | ||
|
||
-- Data | ||
-- data HTTPURL = MkHTTPURL String String | ||
public export | ||
QueryParam : Type | ||
QueryParam = (String, String) | ||
|
||
public export | ||
record HTTPURL where | ||
constructor MkHTTPURL | ||
scheme, host, port, path, query : String | ||
scheme, host : String | ||
port : Maybe String | ||
path : String | ||
|
||
query : List QueryParam | ||
|
||
fragment : Maybe String | ||
|
||
public export | ||
Show HTTPURL where | ||
show (MkHTTPURL scheme host port path query) = "MkHTTPURL \"" ++ scheme ++ "\" \"" | ||
show (MkHTTPURL scheme host port path query fragment) = "MkHTTPURL \"" ++ scheme ++ "\" \"" | ||
++ host ++ "\" \"" | ||
++ port ++ "\" \"" | ||
++ show port ++ "\" \"" | ||
++ path ++ "\" \"" | ||
++ query ++ "\"" | ||
++ show query ++ "\" \"" | ||
++ show fragment ++ "\"" | ||
|
||
public export | ||
Eq HTTPURL where | ||
(MkHTTPURL scheme host port path query) == (MkHTTPURL scheme1 host1 port1 path1 query1) = | ||
(MkHTTPURL scheme host port path query fragment) == (MkHTTPURL scheme1 host1 port1 path1 query1 fragment1) = | ||
scheme == scheme1 | ||
&& host == host1 | ||
&& port == port1 | ||
&& path == path1 | ||
&& query == query1 | ||
&& fragment == fragment1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,22 +10,41 @@ import Network.URL.HTTP.Parser | |
private | ||
testParser : List Test | ||
testParser = [ | ||
test "测试解析1" $ assertEq (parse "http://www.baidu.com") (Just $ MkHTTPURL "http" "www.baidu.com" "" "" "") | ||
, test "测试解析2" $ assertEq (parse "http://127.0.0.1:8080") (Just $ MkHTTPURL "http" "127.0.0.1" "8080" "" "") | ||
, test "测试解析3" $ assertEq (parse "http://127.0.0.1:8080/a/b.html") (Just $ MkHTTPURL "http" "127.0.0.1" "8080" "/a/b.html" "") | ||
, test "测试解析4" $ assertEq (parse "http://127.0.0.1:8080/a/b.html?a=1&b=2") (Just $ MkHTTPURL "http" "127.0.0.1" "8080" "/a/b.html" "a=1&b=2") | ||
test "测试解析1" $ assertEq (parse "http://www.baidu.com") (Just $ MkHTTPURL "http" "www.baidu.com" Nothing "/" [] Nothing) | ||
, test "测试解析2" $ assertEq (parse "http://127.0.0.1:8080") (Just $ MkHTTPURL "http" "127.0.0.1" (Just "8080") "/" [] Nothing) | ||
, test "测试解析3" $ assertEq (parse "http://127.0.0.1:8080/a/b.html") (Just $ MkHTTPURL "http" "127.0.0.1" (Just "8080") "/a/b.html" [] Nothing) | ||
, test "测试解析4" $ assertEq (parse "http://127.0.0.1:8080/a/b.html?a=1&b=2") (Just $ MkHTTPURL "http" "127.0.0.1" (Just "8080") "/a/b.html" [("a","1"),("b","2")] Nothing) | ||
, test "测试解析5" $ assertEq (parse "http://127.0.0.1:8080/a/b.html?a=1&b=2#hello-world") (Just $ MkHTTPURL "http" "127.0.0.1" (Just "8080") "/a/b.html" [("a","1"),("b","2")] (Just "hello-world")) | ||
, test "测试解析6" $ assertEq (parse "http://127.0.0.1:8080/a/b.html#hello-world") (Just $ MkHTTPURL "http" "127.0.0.1" (Just "8080") "/a/b.html" [] (Just "hello-world")) | ||
] | ||
|
||
-- private | ||
-- testStringify : List Test | ||
-- testStringify = [ | ||
-- test "测试解析1" $ assertEq (stringify $ MkGeneralURL "file" "//c:/path/file") "file://c:/path/file" | ||
-- , test "测试解析2" $ assertEq (stringify $ MkGeneralURL "mailto" "[email protected]") "mailto:[email protected]" | ||
-- ] | ||
private | ||
testStringify : List Test | ||
testStringify = [ | ||
test "测试编码1" $ assertEq (stringify $ MkHTTPURL "http" "127.0.0.1" (Just "8080") "/a/b.html" [("a","1"),("b","2")] (Just "hello-world")) "http://127.0.0.1:8080/a/b.html?a=1&b=2#hello-world" | ||
, test "测试编码2" $ assertEq (stringify $ MkHTTPURL "http" "127.0.0.1" (Just "8080") "/a/b.html" [] (Just "hello-world")) "http://127.0.0.1:8080/a/b.html#hello-world" | ||
] | ||
|
||
private | ||
mkUrlByParams : List QueryParam -> HTTPURL | ||
mkUrlByParams qs = MkHTTPURL "https" "grass.show" Nothing "/" qs Nothing | ||
|
||
private | ||
testAddParams : List Test | ||
testAddParams = [ | ||
test "向空列表插入查询参数" $ assertEq test1 $ insertParam ("a","1") test0 | ||
, test "向已有列表插入查询参数" $ assertEq test2 $ insertParam ("b","2") test1 | ||
, test "向已有列表更新查询参数" $ assertEq test1 $ insertParam ("a","1") test11 | ||
] | ||
where | ||
test0 = mkUrlByParams [] | ||
test1 = mkUrlByParams [("a","1")] | ||
test11 = mkUrlByParams [("a","11")] | ||
test2 = mkUrlByParams [("a","1"), ("b","2")] | ||
|
||
export | ||
tests : List Test | ||
tests = testParser | ||
tests = testParser ++ testStringify ++ testAddParams | ||
|
||
private | ||
main : IO Bool | ||
|