Skip to content

Commit

Permalink
1.3.0 - Make simple fetch throw errors on non 200 responses.
Browse files Browse the repository at this point in the history
  • Loading branch information
treeform committed Oct 9, 2021
1 parent 4dbf18e commit e123b69
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion puppy.nimble
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = "1.2.1"
version = "1.3.0"
author = "Andre von Houck"
description = "Puppy fetches HTML pages for Nim."
license = "MIT"
Expand Down
9 changes: 6 additions & 3 deletions src/puppy.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import puppy/common, net, strutils, urlly, zippy
import net, puppy/common, strutils, urlly, zippy

export common, urlly

Expand Down Expand Up @@ -270,9 +270,12 @@ proc newRequest*(
result.headers.merge(headers)
result.timeout = timeout

proc fetch*(url: string, verb = "get", headers = newSeq[Header]()): string =
proc fetch*(url: string, headers = newSeq[Header]()): string =
let
req = newRequest(url, verb, headers)
req = newRequest(url, "get", headers)
res = req.fetch()
if res.code == 200:
return res.body
raise newException(PuppyError,
"Non 200 response code: " & $res.code & "\n" & res.body
)
4 changes: 2 additions & 2 deletions tests/debug_client.nim
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import puppy, zippy


for i in 0 ..< 100:
block:
# test basic
doAssert fetch("http://localhost:8080/ok") == "ok"
doAssert fetch("http://localhost:8080/401") == ""
doAssertRaises(PuppyError):
discard fetch("http://localhost:8080/401")

block:
# test 404
Expand Down
2 changes: 1 addition & 1 deletion tests/debug_server.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import asyncdispatch, asynchttpserver, zippy, uri
import asyncdispatch, asynchttpserver, uri, zippy

let server = newAsyncHttpServer()

Expand Down

0 comments on commit e123b69

Please sign in to comment.