diff --git a/Project.toml b/Project.toml index cc6e5add..cbe11afb 100644 --- a/Project.toml +++ b/Project.toml @@ -22,7 +22,7 @@ Bonito = "^3" CairoMakie = "0.11, 1" DataStructures = "0.18.15, 1" Dates = "^1" -HTTP = "^1" +HTTP = "^1.8" JSON3 = "^1.9" MIMEs = "0.1.4, 1" Mustache = "^1" diff --git a/src/Oxygen.jl b/src/Oxygen.jl index b7eee210..30ff4bf5 100644 --- a/src/Oxygen.jl +++ b/src/Oxygen.jl @@ -13,11 +13,10 @@ include("core.jl"); using .Core include("instances.jl"); using .Instances include("extensions/load.jl"); -import HTTP: Request, Response +import HTTP: Request, Response, Stream, WebSocket, queryparams using .Core: Context, History, Server, Nullable using .Core: GET, POST, PUT, DELETE, PATCH - const CONTEXT = Ref{Context}(Context()) import Base: get @@ -43,7 +42,7 @@ export @oxidise, @get, @post, @put, @patch, @delete, @route, serve, serveparallel, terminate, internalrequest, resetstate, instance, staticfiles, dynamicfiles, # Util - redirect, queryparams, formdata, format_sse_message, + redirect, formdata, format_sse_message, html, text, json, file, xml, js, css, binary, # Extractors Path, Query, Header, Json, JsonFragment, Form, Body, extract, validate, @@ -54,5 +53,5 @@ export @oxidise, @get, @post, @put, @patch, @delete, @route, starttasks, stoptasks, cleartasks, startcronjobs, stopcronjobs, clearcronjobs, # Common HTTP Types - Request, Response, Stream, WebSocket + Request, Response, Stream, WebSocket, queryparams end diff --git a/src/types.jl b/src/types.jl index 7d738ffa..dc2c8085 100644 --- a/src/types.jl +++ b/src/types.jl @@ -117,7 +117,7 @@ end function queryvars(req::LazyRequest) :: Nullable{Dict{String,String}} if isnothing(req.queryparams[]) - req.queryparams[] = Util.queryparams(req.request) + req.queryparams[] = HTTP.queryparams(req.request) end return req.queryparams[] end diff --git a/src/utilities/bodyparsers.jl b/src/utilities/bodyparsers.jl index 8d42852e..72549cba 100644 --- a/src/utilities/bodyparsers.jl +++ b/src/utilities/bodyparsers.jl @@ -24,7 +24,7 @@ end Read the html form data from the body of a HTTP.Request """ function formdata(req::HTTP.Request) :: Dict - return HTTP.URIs.queryparams(text(req)) + return HTTP.queryparams(text(req)) end @@ -78,7 +78,7 @@ end Read the html form data from the body of a HTTP.Response """ function formdata(response::HTTP.Response) :: Dict - return HTTP.URIs.queryparams(text(response)) + return HTTP.queryparams(text(response)) end diff --git a/src/utilities/misc.jl b/src/utilities/misc.jl index 3ccd8ee1..37aa21b7 100644 --- a/src/utilities/misc.jl +++ b/src/utilities/misc.jl @@ -5,21 +5,11 @@ using Dates using ..Errors: ValidationError export countargs, recursive_merge, parseparam, - queryparams, redirect, handlerequest, + redirect, handlerequest, format_response!, set_content_size!, format_sse_message ### Request helper functions ### -""" - queryparams(request::HTTP.Request) - -Parse's the query parameters from the Requests URL and return them as a Dict -""" -function queryparams(req::HTTP.Request) :: Dict - local uri = HTTP.URI(req.target) - return HTTP.queryparams(uri.query) -end - """ redirect(path::String; code = 308) diff --git a/test/bodyparsertests.jl b/test/bodyparsertests.jl index dcf53f9b..b0fca0d7 100644 --- a/test/bodyparsertests.jl +++ b/test/bodyparsertests.jl @@ -1,7 +1,6 @@ module BodyParserTests using Test -using HTTP using StructTypes using Oxygen @@ -15,18 +14,29 @@ end # added supporting structype StructTypes.StructType(::Type{rank}) = StructTypes.Struct() -req = HTTP.Request("GET", "/json", [], """{"message":["hello",1.0]}""") +req = Request("GET", "/json", [], """{"message":["hello",1.0]}""") json(req) +@testset "queryparams" begin + no_params = Request("GET", "http://google.com") + with_params = Request("GET", "http://google.com?q=123&l=345") + + @test queryparams(no_params) == Dict{String, String}() + @test queryparams(with_params) == Dict("q" => "123", "l" => "345") + + @test queryparams(Response(200; body="", request=with_params)) == Dict("q" => "123", "l" => "345") + @test isnothing(queryparams(Response(200; body=""))) +end + @testset "formdata() Request struct keyword tests" begin - req = HTTP.Request("POST", "/", [], "message=hello world&value=3") + req = Request("POST", "/", [], "message=hello world&value=3") data = formdata(req) @test data["message"] == "hello world" @test data["value"] == "3" end @testset "formdata() Response struct keyword tests" begin - req = HTTP.Response("message=hello world&value=3") + req = Response("message=hello world&value=3") data = formdata(req) @test data["message"] == "hello world" @test data["value"] == "3" @@ -79,14 +89,14 @@ end @testset "json() Request struct keyword tests" begin - req = HTTP.Request("GET", "/json", [], "{\"message\":[NaN,1.0]}") + req = Request("GET", "/json", [], "{\"message\":[NaN,1.0]}") @test isnan(json(req, allow_inf = true)["message"][1]) @test !isnan(json(req, allow_inf = true)["message"][2]) - req = HTTP.Request("GET", "/json", [], "{\"message\":[Inf,1.0]}") + req = Request("GET", "/json", [], "{\"message\":[Inf,1.0]}") @test isinf(json(req, allow_inf = true)["message"][1]) - req = HTTP.Request("GET", "/json", [], "{\"message\":[null,1.0]}") + req = Request("GET", "/json", [], "{\"message\":[null,1.0]}") @test isnothing(json(req, allow_inf = false)["message"][1]) end @@ -94,11 +104,11 @@ end @testset "json() Request stuct keyword with classtype" begin - req = HTTP.Request("GET","/", [],"""{"title": "viscount", "power": NaN}""") + req = Request("GET","/", [],"""{"title": "viscount", "power": NaN}""") myjson = json(req, rank, allow_inf = true) @test isnan(myjson.power) - req = HTTP.Request("GET","/", [],"""{"title": "viscount", "power": 9000.1}""") + req = Request("GET","/", [],"""{"title": "viscount", "power": 9000.1}""") myjson = json(req, rank, allow_inf = false) @test myjson.power == 9000.1 @@ -107,39 +117,39 @@ end @testset "regular Request json() tests" begin - req = HTTP.Request("GET", "/json", [], "{\"message\":[null,1.0]}") + req = Request("GET", "/json", [], "{\"message\":[null,1.0]}") @test isnothing(json(req)["message"][1]) @test json(req)["message"][2] == 1 - req = HTTP.Request("GET", "/json", [], """{"message":["hello",1.0]}""") + req = Request("GET", "/json", [], """{"message":["hello",1.0]}""") @test json(req)["message"][1] == "hello" @test json(req)["message"][2] == 1 - req = HTTP.Request("GET", "/json", [], "{\"message\":[3.4,4.0]}") + req = Request("GET", "/json", [], "{\"message\":[3.4,4.0]}") @test json(req)["message"][1] == 3.4 @test json(req)["message"][2] == 4 - req = HTTP.Request("GET", "/json", [], "{\"message\":[null,1.0]}") + req = Request("GET", "/json", [], "{\"message\":[null,1.0]}") @test isnothing(json(req)["message"][1]) end @testset "json() Request with classtype" begin - req = HTTP.Request("GET","/", [],"""{"title": "viscount", "power": NaN}""") + req = Request("GET","/", [],"""{"title": "viscount", "power": NaN}""") myjson = json(req, rank) @test isnan(myjson.power) - req = HTTP.Request("GET","/", [],"""{"title": "viscount", "power": 9000.1}""") + req = Request("GET","/", [],"""{"title": "viscount", "power": 9000.1}""") myjson = json(req, rank) @test myjson.power == 9000.1 # test invalid json - req = HTTP.Request("GET","/", [],"""{}""") + req = Request("GET","/", [],"""{}""") @test_throws MethodError json(req, rank) # test extra key - req = HTTP.Request("GET","/", [],"""{"title": "viscount", "power": 9000.1, "extra": "hi"}""") + req = Request("GET","/", [],"""{"title": "viscount", "power": 9000.1, "extra": "hi"}""") myjson = json(req, rank) @test myjson.power == 9000.1 @@ -148,11 +158,11 @@ end @testset "json() Response" begin - res = HTTP.Response("""{"title": "viscount", "power": 9000.1}""") + res = Response("""{"title": "viscount", "power": 9000.1}""") myjson = json(res) @test myjson["power"] == 9000.1 - res = HTTP.Response("""{"title": "viscount", "power": 9000.1}""") + res = Response("""{"title": "viscount", "power": 9000.1}""") myjson = json(res, rank) @test myjson.power == 9000.1 @@ -160,14 +170,14 @@ end @testset "json() Response struct keyword tests" begin - req = HTTP.Response("{\"message\":[NaN,1.0]}") + req = Response("{\"message\":[NaN,1.0]}") @test isnan(json(req, allow_inf = true)["message"][1]) @test !isnan(json(req, allow_inf = true)["message"][2]) - req = HTTP.Response("{\"message\":[Inf,1.0]}") + req = Response("{\"message\":[Inf,1.0]}") @test isinf(json(req, allow_inf = true)["message"][1]) - req = HTTP.Response("{\"message\":[null,1.0]}") + req = Response("{\"message\":[null,1.0]}") @test isnothing(json(req, allow_inf = false)["message"][1]) end @@ -175,11 +185,11 @@ end @testset "json() Response stuct keyword with classtype" begin - req = HTTP.Response("""{"title": "viscount", "power": NaN}""") + req = Response("""{"title": "viscount", "power": NaN}""") myjson = json(req, rank, allow_inf = true) @test isnan(myjson.power) - req = HTTP.Response("""{"title": "viscount", "power": 9000.1}""") + req = Response("""{"title": "viscount", "power": 9000.1}""") myjson = json(req, rank, allow_inf = false) @test myjson.power == 9000.1 @@ -188,39 +198,39 @@ end @testset "regular json() Response tests" begin - req = HTTP.Response("{\"message\":[null,1.0]}") + req = Response("{\"message\":[null,1.0]}") @test isnothing(json(req)["message"][1]) @test json(req)["message"][2] == 1 - req = HTTP.Response("""{"message":["hello",1.0]}""") + req = Response("""{"message":["hello",1.0]}""") @test json(req)["message"][1] == "hello" @test json(req)["message"][2] == 1 - req = HTTP.Response("{\"message\":[3.4,4.0]}") + req = Response("{\"message\":[3.4,4.0]}") @test json(req)["message"][1] == 3.4 @test json(req)["message"][2] == 4 - req = HTTP.Response("{\"message\":[null,1.0]}") + req = Response("{\"message\":[null,1.0]}") @test isnothing(json(req)["message"][1]) end @testset "json() Response with classtype" begin - req = HTTP.Response("""{"title": "viscount", "power": NaN}""") + req = Response("""{"title": "viscount", "power": NaN}""") myjson = json(req, rank) @test isnan(myjson.power) - req = HTTP.Response("""{"title": "viscount", "power": 9000.1}""") + req = Response("""{"title": "viscount", "power": 9000.1}""") myjson = json(req, rank) @test myjson.power == 9000.1 # test invalid json - req = HTTP.Response("""{}""") + req = Response("""{}""") @test_throws MethodError json(req, rank) # test extra key - req = HTTP.Response("""{"title": "viscount", "power": 9000.1, "extra": "hi"}""") + req = Response("""{"title": "viscount", "power": 9000.1, "extra": "hi"}""") myjson = json(req, rank) @test myjson.power == 9000.1 diff --git a/test/runtests.jl b/test/runtests.jl index f02cb0c8..9d8ad460 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -18,7 +18,7 @@ include("streamingtests.jl") include("handlertests.jl") # #### Core Tests #### - +include("test_reexports.jl") include("precompilationtest.jl") include("extractortests.jl") include("reflectiontests.jl") diff --git a/test/test_reexports.jl b/test/test_reexports.jl new file mode 100644 index 00000000..04212df9 --- /dev/null +++ b/test/test_reexports.jl @@ -0,0 +1,14 @@ +module TestReexports +using Test +import HTTP +import Oxygen + +@testset "Testing HTTP Reexports" begin + @test Oxygen.Request == HTTP.Request + @test Oxygen.Response == HTTP.Response + @test Oxygen.Stream == HTTP.Stream + @test Oxygen.WebSocket == HTTP.WebSocket + @test Oxygen.queryparams == HTTP.queryparams +end + +end \ No newline at end of file