Skip to content

Commit c92f7f1

Browse files
committed
update tests
1 parent bdac8dc commit c92f7f1

3 files changed

Lines changed: 154 additions & 167 deletions

File tree

src/Merly.jl

Lines changed: 2 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -6,162 +6,12 @@ JSON,
66
HTTP#,
77
#XMLDict
88

9+
include("base.jl")
910
include("mimetypes.jl")
1011
include("routes.jl")
1112
include("allformats.jl")
1213
include("webserver.jl")
1314

1415
export app, @page, @route, GET,POST,PUT,DELETE,HEAD,OPTIONS,PATCH,Get,Post,Put,Delete
1516

16-
mutable struct myresponse
17-
status::Int
18-
headers::Dict
19-
body::String
20-
end
21-
22-
my_headers= HTTP.mkheaders(["Content-Type" => "text/plain"])
23-
24-
cors=false::Bool
25-
root=pwd()
26-
if root[end]=='/'
27-
root=root[1:end-1]
28-
elseif Sys.iswindows() && root[end]=='\\'
29-
root=root[1:end-1]
30-
end
31-
32-
exten="\"\""::AbstractString
33-
34-
mutable struct Data
35-
query::Dict
36-
params::Any
37-
body::Any
38-
end
39-
global notfound_message = "NotFound"::String
40-
global q=Data(Dict(),"","")
41-
42-
mutable struct Fram
43-
notfound::Function
44-
start::Function
45-
useCORS::Function
46-
webserverfiles::Function
47-
webserverpath::Function
48-
end
49-
50-
function _body(data::Array{UInt8,1},format::SubString{String})
51-
return getindex(formats, format)(String(data))
52-
end
53-
54-
function File(file::String)
55-
path = normpath(root, file)
56-
return String(read(path))
57-
end
58-
59-
function resolveroute(ruta::String)
60-
for key in keys(routes_patterns)
61-
params= match(key,ruta)
62-
if params!= nothing
63-
return params, getindex(routes_patterns,key)
64-
end
65-
end
66-
end
67-
68-
function processroute_pattern(searchroute::String,request,response)
69-
q.params, _function = resolveroute(searchroute)
70-
respond = _function(q,request,response)
71-
sal = collect((m.match for m = eachmatch(Regex("{{([a-z])+}}"), respond)))
72-
for i in sal
73-
respond = replace(respond,Regex(i) => q.params["$(i[3:end-2])"])
74-
end
75-
response.status = 200
76-
return respond
77-
end
78-
79-
function handler(request::HTTP.Messages.Request)
80-
data = split(request.target,"?")
81-
url=data[1]
82-
searchroute = request.method*url
83-
84-
if (length(data)>1) q.query= HTTP.queryparams(data[2]) end
85-
86-
response = myresponse(200,Dict(),"")
87-
88-
if ((request.method=="POST" )||(request.method=="PUT" )||(request.method=="PATCH"))
89-
header_content_type = HTTP.header(request, "Content-Type")
90-
if(length(header_content_type)>0)
91-
q.body= getindex(formats, header_content_type)(String(request.body))
92-
else
93-
q.body = getindex(formats, "*/*")(String(request.body))
94-
end
95-
end
96-
97-
if (searchroute in routes_array)
98-
response.body = getindex(routes, searchroute)(q,request,response)
99-
else
100-
#try
101-
# response.body = processroute_pattern(searchroute,request,response)
102-
#catch
103-
response.body = getindex(routes, "notfound")(q,request,response)
104-
#end
105-
end
106-
107-
responsefinal = HTTP.Response(response.status,my_headers, body=response.body)
108-
109-
for (key, value) in response.headers
110-
HTTP.setheader(responsefinal,key => value )
111-
end
112-
113-
return responsefinal
114-
end
115-
116-
117-
function app()
118-
global root
119-
global exten
120-
global cors
121-
122-
function useCORS(activate::Bool)
123-
HTTP.setheader(my_headers,"Access-Control-Allow-Origin" => "*")
124-
HTTP.setheader(my_headers,"Access-Control-Allow-Methods" => "POST,GET,OPTIONS")
125-
return true
126-
end
127-
128-
function notfound(text::String)
129-
if occursin(".html", text)
130-
notfound_message= File(text)
131-
else
132-
notfound_message= text
133-
end
134-
end
135-
136-
function webserverfiles(load::AbstractString)
137-
if load=="*"
138-
WebServer(root)
139-
else
140-
exten=load::AbstractString
141-
WebServer(root)
142-
end
143-
end
144-
145-
function webserverpath(path::AbstractString)
146-
root= path
147-
end
148-
149-
function start(config=Dict("host" => "127.0.0.1","port" => 8000)::Dict)
150-
host= Sockets.IPv4("127.0.0.1")
151-
port=get(config, "port", 8000)::Int
152-
153-
my_host = get(config, "host", "127.0.0.1")::String
154-
if ('.' in my_host) host=Sockets.IPv4(my_host) end
155-
if (':' in my_host) host=Sockets.IPv6(my_host) end
156-
157-
http = (req)-> handler(req)
158-
myserver= HTTP.Servers.Server(http, stdout)
159-
@info("Listening on: $(host) : $(port)")
160-
return HTTP.Servers.serve(myserver, host, port)
161-
end
162-
163-
@info("App created")
164-
return Fram(notfound,start,useCORS,webserverfiles,webserverpath)
165-
end
166-
end # module
167-
#HSTS HTTP.setheader(response,"Strict-Transport-Security" => "max-age=10886400; includeSubDomains; preload"
17+
end # module

src/base.jl

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
mutable struct myresponse
2+
status::Int
3+
headers::Dict
4+
body::String
5+
end
6+
7+
mutable struct Data
8+
query::Dict
9+
params::Any
10+
body::Any
11+
end
12+
13+
struct Fram
14+
notfound::Function
15+
start::Function
16+
useCORS::Function
17+
webserverfiles::Function
18+
webserverpath::Function
19+
end
20+
21+
my_headers= HTTP.mkheaders(["Content-Type" => "text/plain"])
22+
root=pwd()
23+
if root[end]=='/'
24+
root=root[1:end-1]
25+
elseif Sys.iswindows() && root[end]=='\\'
26+
root=root[1:end-1]
27+
end
28+
exten="\"\""::AbstractString
29+
notfound_message = "NotFound"::String
30+
q=Data(Dict(),"","")
31+
32+
function File(file::String)
33+
path = normpath(root, file)
34+
return String(read(path))
35+
end
36+
37+
function resolveroute(ruta::String)
38+
for key in keys(routes_patterns)
39+
params= match(key,ruta)
40+
if params!= nothing
41+
return params, getindex(routes_patterns,key)
42+
end
43+
end
44+
end
45+
46+
function processroute_pattern(searchroute::String,request,response)
47+
q.params, _function = resolveroute(searchroute)
48+
respond = _function(q,request,response)
49+
sal = collect((m.match for m = eachmatch(Regex("{{([a-z])+}}"), respond)))
50+
for i in sal
51+
respond = replace(respond,Regex(i) => q.params["$(i[3:end-2])"])
52+
end
53+
response.status = 200
54+
return respond
55+
end
56+
57+
function handler(request::HTTP.Messages.Request)
58+
data = split(request.target,"?")
59+
url=data[1]
60+
searchroute = request.method*url
61+
if (length(data)>1) q.query= HTTP.queryparams(data[2]) end
62+
response = myresponse(200,Dict(),"")
63+
64+
if ((request.method=="POST" )||(request.method=="PUT" )||(request.method=="PATCH"))
65+
header_content_type = HTTP.header(request, "Content-Type")
66+
if(length(header_content_type)>0)
67+
q.body= getindex(formats, header_content_type)(String(request.body))
68+
else
69+
q.body = getindex(formats, "*/*")(String(request.body))
70+
end
71+
end
72+
73+
if (searchroute in routes_array)
74+
response.body = getindex(routes, searchroute)(q,request,response)
75+
else
76+
#try
77+
# response.body = processroute_pattern(searchroute,request,response)
78+
#catch
79+
response.body = getindex(routes, "notfound")(q,request,response)
80+
#end
81+
end
82+
responsefinal = HTTP.Response(response.status,my_headers, body=response.body)
83+
for (key, value) in response.headers
84+
HTTP.setheader(responsefinal,key => value )
85+
end
86+
return responsefinal
87+
end
88+
89+
90+
function app()
91+
global exten
92+
93+
function useCORS(activate::Bool)
94+
HTTP.setheader(my_headers,"Access-Control-Allow-Origin" => "*")
95+
HTTP.setheader(my_headers,"Access-Control-Allow-Methods" => "POST,GET,OPTIONS")
96+
return true
97+
end
98+
99+
function notfound(text::String)
100+
if occursin(".html", text)
101+
notfound_message= File(text)
102+
else
103+
notfound_message= text
104+
end
105+
end
106+
107+
function webserverfiles(load::AbstractString)
108+
if load=="*"
109+
WebServer(root)
110+
else
111+
exten=load::AbstractString
112+
WebServer(root)
113+
end
114+
end
115+
116+
function webserverpath(path::AbstractString)
117+
root= path
118+
end
119+
120+
function start(config=Dict("host" => "127.0.0.1","port" => 8000)::Dict)
121+
host= Sockets.IPv4("127.0.0.1")
122+
port=get(config, "port", 8000)::Int
123+
my_host = get(config, "host", "127.0.0.1")::String
124+
if ('.' in my_host) host=Sockets.IPv4(my_host) end
125+
if (':' in my_host) host=Sockets.IPv6(my_host) end
126+
http = (req)-> handler(req)
127+
myserver= HTTP.Servers.Server(http, stdout)
128+
@info("Listening on: $(host) : $(port)")
129+
return HTTP.Servers.serve(myserver, host, port)
130+
end
131+
132+
@info("App created")
133+
return Fram(notfound,start,useCORS,webserverfiles,webserverpath)
134+
end
135+
136+
#HSTS HTTP.setheader(response,"Strict-Transport-Security" => "max-age=10886400; includeSubDomains; preload"

test/runtests.jl

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ using JSON
77
#using BenchmarkTools
88

99
ip = "127.0.0.1" #127.0.0.1
10+
port = 8086
1011
# write your own tests here
1112
server = Merly.app()
1213

@@ -63,69 +64,69 @@ end))
6364

6465
server.webserverfiles("jl")
6566

66-
@async server.start(Dict("host" => "$(ip)","port" => 8000))
67+
@async server.start(Dict("host" => "$(ip)","port" => port))
6768

6869

69-
r = HTTP.get("http://$(ip):8000/")
70+
r = HTTP.get("http://$(ip):$(port)/")
7071
@test r.status == 200
7172
@test String(r.body) == "Hello World!"
7273

73-
#=r = HTTP.get("http://$(ip):8000/hola/usuario")
74+
#=r = HTTP.get("http://$(ip):$(port)/hola/usuario")
7475
@test r.status == 200
7576
@test String(r.body) == "<b>Hello usuario!</b>"
7677
77-
r = HTTP.get("http://$(ip):8000/get/testdata")
78+
r = HTTP.get("http://$(ip):$(port)/get/testdata")
7879
@test r.status == 200
7980
@test String(r.body) == "get this back: testdata"
8081
=#
81-
r = HTTP.get("http://$(ip):8000/data?hola=1")
82+
r = HTTP.get("http://$(ip):$(port)/data?hola=1")
8283
@test r.status == 200
8384
@test String(r.body) == "hellodata"
8485

8586
myjson = Dict("query"=>"data")
8687
my_headers = HTTP.mkheaders(["Accept" => "application/json","Content-Type" => "application/json"])
87-
r = HTTP.post("http://$(ip):8000/data",my_headers,JSON.json(myjson))
88+
r = HTTP.post("http://$(ip):$(port)/data",my_headers,JSON.json(myjson))
8889
@test r.status == 200
8990
@test String(r.body) == "I did something!"
9091

91-
r = HTTP.get("http://$(ip):8000/data")
92+
r = HTTP.get("http://$(ip):$(port)/data")
9293
@test r.status == 200
9394
@test String(r.body) == "byedata"
9495

9596

9697

97-
r = HTTP.post("http://$(ip):8000/post",my_headers,JSON.json(myjson))
98+
r = HTTP.post("http://$(ip):$(port)/post",my_headers,JSON.json(myjson))
9899
@test r.status == 200
99100
@test String(r.body) == "I did something!"
100101

101-
r = HTTP.post("http://$(ip):8000/",my_headers,JSON.json(myjson))
102+
r = HTTP.post("http://$(ip):$(port)/",my_headers,JSON.json(myjson))
102103
@test r.status == 200
103104
@test String(r.body) == "I did something!"
104105

105-
r = HTTP.put("http://$(ip):8000/",my_headers,JSON.json(myjson))
106+
r = HTTP.put("http://$(ip):$(port)/",my_headers,JSON.json(myjson))
106107
@test r.status == 200
107108
@test String(r.body) == "I did something!"
108109

109-
r = HTTP.delete("http://$(ip):8000/")
110+
r = HTTP.delete("http://$(ip):$(port)/")
110111
@test r.status == 200
111112
@test String(r.body) == "I did something!"
112113
@test r.headers == Pair{SubString{String},SubString{String}}["Content-Type"=>"text/plain", "Access-Control-Allow-Origin"=>"*", "Access-Control-Allow-Methods"=>"POST,GET,OPTIONS", "Transfer-Encoding"=>"chunked"]
113114

114115
try
115-
r = HTTP.get("http://$(ip):8000/nada")
116+
r = HTTP.get("http://$(ip):$(port)/nada")
116117
catch e
117118
@test e.status == 404
118119
@test String(e.response.body) == "NotFound"
119120
end
120121

121-
r = HTTP.get("http://$(ip):8000/prueba.txt")
122+
r = HTTP.get("http://$(ip):$(port)/prueba.txt")
122123
@test r.status == 200
123124
@test String(r.body) == "probando webserver"
124125
@test r.headers == Pair{SubString{String},SubString{String}}["Content-Type"=>"text/plain", "Access-Control-Allow-Origin"=>"*", "Access-Control-Allow-Methods"=>"POST,GET,OPTIONS", "Transfer-Encoding"=>"chunked"]
125126

126-
r = HTTP.get("http://$(ip):8000/index.html")
127+
r = HTTP.get("http://$(ip):$(port)/index.html")
127128
@test r.status == 200
128129
@test String(r.body) == "<!DOCTYPE html>\r\n<html lang=\"en\">\r\n<head>\r\n\t<meta charset=\"UTF-8\">\r\n\t<title>Document</title>\r\n</head>\r\n<body>\r\n<h1>hola</h1>\r\n</body>\r\n</html>"
129130
@test r.headers == Pair{SubString{String},SubString{String}}["Content-Type"=>"text/html", "Access-Control-Allow-Origin"=>"*", "Access-Control-Allow-Methods"=>"POST,GET,OPTIONS", "Transfer-Encoding"=>"chunked"]
130131

131-
#@btime HTTP.get("http://$(ip):8000/?hola=5")
132+
#@btime HTTP.get("http://$(ip):$(port)/?hola=5")

0 commit comments

Comments
 (0)