Skip to content

Commit

Permalink
feature/reexport-queryparams (#229)
Browse files Browse the repository at this point in the history
1.) now reexporting queryparams function from HTTP (bumped compat version to 1.8
2.) added reexport tests file
3.) bodyparser utils now use the queryparams function from the top level http function
4.) body parser tests now only uses the reexports from oxygen
  • Loading branch information
ndortega authored Oct 17, 2024
1 parent 793d163 commit bb72867
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 52 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 3 additions & 4 deletions src/Oxygen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
2 changes: 1 addition & 1 deletion src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/utilities/bodyparsers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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


Expand Down
12 changes: 1 addition & 11 deletions src/utilities/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
74 changes: 42 additions & 32 deletions test/bodyparsertests.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

module BodyParserTests
using Test
using HTTP
using StructTypes

using Oxygen
Expand All @@ -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"
Expand Down Expand Up @@ -79,26 +89,26 @@ 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


@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

Expand All @@ -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

Expand All @@ -148,38 +158,38 @@ 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

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


@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

Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
14 changes: 14 additions & 0 deletions test/test_reexports.jl
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit bb72867

Please sign in to comment.