This repository was archived by the owner on Sep 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathruntests.jl
153 lines (133 loc) · 5.6 KB
/
runtests.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using HttpCommon
using FactCheck
using HttpServer
facts("HttpServer utility functions:") do
context("`write` correctly writes data response") do
response = Response(200, "Hello World!")
buf = IOBuffer();
HttpServer.write(buf, response)
response_string = String(take!(buf))
vals = split(response_string, "\r\n")
grep(a::Array, k::AbstractString) = filter(x -> ismatch(Regex(k), x), a)[1]
@fact grep(vals, "HTTP") --> "HTTP/1.1 200 OK "
@fact grep(vals, "Server") --> "Server: Julia/$VERSION"
# default to text/html
@fact grep(vals, "Content-Type") --> "Content-Type: text/html; charset=utf-8"
# skip date
@fact grep(vals, "Content-Language") --> "Content-Language: en"
@fact grep(vals, "Hello") --> "Hello World!"
end
end
import Requests: get, text, statuscode
facts("HttpServer runs") do
context("using HTTP protocol on 0.0.0.0:8000") do
http = HttpHandler() do req::Request, res::Response
res = Response( ismatch(r"^/hello/",req.resource) ? string("Hello ", split(req.resource,'/')[3], "!") : 404 )
setcookie!(res, "sessionkey", "abc", Dict("Path"=>"/test", "Secure"=>""))
end
server = Server(http)
@async run(server, 8000)
sleep(1.0)
ret = Requests.get("http://localhost:8000/hello/travis")
@fact text(ret) --> "Hello travis!"
@fact statuscode(ret) --> 200
@fact haskey(ret.cookies, "sessionkey") --> true
let cookie = ret.cookies["sessionkey"]
@fact cookie.value --> "abc"
@fact cookie.attrs["Path"] --> "/test"
@fact haskey(cookie.attrs, "Secure") --> true
end
ret = Requests.get("http://localhost:8000/bad")
@fact text(ret) --> ""
@fact statuscode(ret) --> 404
close(server)
end
context("Rerun test using HTTP protocol on 0.0.0.0:8000 after closing") do
http = HttpHandler() do req::Request, res::Response
res = Response( ismatch(r"^/hello/",req.resource) ? string("Hello ", split(req.resource,'/')[3], "!") : 404 )
setcookie!(res, "sessionkey", "abc", Dict("Path"=>"/test", "Secure"=>""))
end
server = Server(http)
@async run(server, 8000)
sleep(1.0)
ret = Requests.get("http://localhost:8000/hello/travis")
@fact text(ret) --> "Hello travis!"
@fact statuscode(ret) --> 200
@fact haskey(ret.cookies, "sessionkey") --> true
let cookie = ret.cookies["sessionkey"]
@fact cookie.value --> "abc"
@fact cookie.attrs["Path"] --> "/test"
@fact haskey(cookie.attrs, "Secure") --> true
end
ret = Requests.get("http://localhost:8000/bad")
@fact text(ret) --> ""
@fact statuscode(ret) --> 404
close(server)
end
context("using HTTP protocol on 127.0.0.1:8001") do
http = HttpHandler() do req::Request, res::Response
Response( ismatch(r"^/hello/",req.resource) ? string("Hello ", split(req.resource,'/')[3], "!") : 404 )
end
server = Server(http)
@async run(server, host=ip"127.0.0.1", port=8001)
sleep(1.0)
ret = Requests.get("http://127.0.0.1:8001/hello/travis")
@fact text(ret) --> "Hello travis!"
@fact statuscode(ret) --> 200
close(server)
end
context("Rerun test using HTTP protocol on 127.0.0.1:8001 after closing") do
http = HttpHandler() do req::Request, res::Response
Response( ismatch(r"^/hello/",req.resource) ? string("Hello ", split(req.resource,'/')[3], "!") : 404 )
end
server = Server(http)
@async run(server, host=ip"127.0.0.1", port=8001)
sleep(1.0)
ret = Requests.get("http://127.0.0.1:8001/hello/travis")
@fact text(ret) --> "Hello travis!"
@fact statuscode(ret) --> 200
close(server)
end
context("Testing HTTPS on port 8002") do
http = HttpHandler() do req, res
Response("hello")
end
server = Server(http)
cert = MbedTLS.crt_parse_file(joinpath(dirname(@__FILE__),"cert.pem"))
key = MbedTLS.parse_keyfile(joinpath(dirname(@__FILE__),"key.pem"))
@async run(server, port=8002, ssl=(cert, key))
sleep(1.0)
client_tls_conf = Requests.TLS_VERIFY
MbedTLS.ca_chain!(client_tls_conf, cert)
ret = Requests.get("https://localhost:8002", tls_conf=client_tls_conf)
@fact text(ret) --> "hello"
close(server)
end
context("Rerun test of HTTPS on port 8002 after closing") do
http = HttpHandler() do req, res
Response("hello")
end
server = Server(http)
cert = MbedTLS.crt_parse_file(joinpath(dirname(@__FILE__),"cert.pem"))
key = MbedTLS.parse_keyfile(joinpath(dirname(@__FILE__),"key.pem"))
@async run(server, port=8002, ssl=(cert, key))
sleep(1.0)
client_tls_conf = Requests.TLS_VERIFY
MbedTLS.ca_chain!(client_tls_conf, cert)
ret = Requests.get("https://localhost:8002", tls_conf=client_tls_conf)
@fact text(ret) --> "hello"
close(server)
end
# Issue #111
context("Parse HTTP headers") do
http = HttpHandler() do req::Request, res::Response
Response(req.headers["Content-Type"])
end
server = Server(http)
@async run(server, 8000)
sleep(1.0)
ret = Requests.post("http://localhost:8000/", data = "√", headers = Dict("Content-Type" => "text/plain"))
@fact text(ret) --> "text/plain"
close(server)
end
end