Skip to content

Commit 7882ea9

Browse files
committed
handle cached legacy heads
1 parent 509b7b7 commit 7882ea9

File tree

8 files changed

+115
-1
lines changed

8 files changed

+115
-1
lines changed

lib/sinew/response.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ module Sinew
66
class Response
77
attr_accessor :request, :uri, :body, :code, :headers
88

9+
#
10+
# factory methods
11+
#
12+
913
def self.from_network(request, party_response)
1014
Response.new.tap do |response|
1115
response.request = request
@@ -34,6 +38,9 @@ def self.from_cache(request, body, head)
3438

3539
# overwrite with cached response headers
3640
if head
41+
if head !~ /^{/
42+
return from_legacy_head(response, head)
43+
end
3744
head = JSON.parse(head, symbolize_names: true)
3845
response.uri = URI.parse(head[:uri])
3946
response.code = head[:code]
@@ -52,6 +59,26 @@ def self.from_timeout(request)
5259
end
5360
end
5461

62+
def self.from_legacy_head(response, head)
63+
response.tap do |response|
64+
case head
65+
when /\ACURLER_ERROR/
66+
# error
67+
response.code = 999
68+
when /\AHTTP/
69+
# redirect
70+
location = head.scan(/Location: ([^\r\n]+)/).flatten.last
71+
response.uri += location
72+
else
73+
$stderr.puts "unknown cached /head for #{response.uri}"
74+
end
75+
end
76+
end
77+
78+
#
79+
# accessors
80+
#
81+
5582
def error?
5683
code >= 400
5784
end

lib/sinew/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module Sinew
22
# Gem version
3-
VERSION = '2.0.0'.freeze
3+
VERSION = '2.0.1'.freeze
44
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
HTTP/1.1 302 FOUND
2+
Connection: keep-alive
3+
Server: gunicorn/19.7.1
4+
Date: Wed, 02 May 2018 20:55:20 GMT
5+
Content-Type: text/html; charset=utf-8
6+
Content-Length: 247
7+
Location: /relative-redirect/2
8+
Access-Control-Allow-Origin: *
9+
Access-Control-Allow-Credentials: true
10+
X-Powered-By: Flask
11+
X-Processed-Time: 0
12+
Via: 1.1 vegur
13+
14+
HTTP/1.1 302 FOUND
15+
Connection: keep-alive
16+
Server: gunicorn/19.7.1
17+
Date: Wed, 02 May 2018 20:55:20 GMT
18+
Content-Type: text/html; charset=utf-8
19+
Content-Length: 0
20+
Location: /relative-redirect/1
21+
Access-Control-Allow-Origin: *
22+
Access-Control-Allow-Credentials: true
23+
X-Powered-By: Flask
24+
X-Processed-Time: 0
25+
Via: 1.1 vegur
26+
27+
HTTP/1.1 302 FOUND
28+
Connection: keep-alive
29+
Server: gunicorn/19.7.1
30+
Date: Wed, 02 May 2018 20:55:20 GMT
31+
Content-Type: text/html; charset=utf-8
32+
Content-Length: 0
33+
Location: /get
34+
Access-Control-Allow-Origin: *
35+
Access-Control-Allow-Credentials: true
36+
X-Powered-By: Flask
37+
X-Processed-Time: 0
38+
Via: 1.1 vegur
39+
40+
HTTP/1.1 200 OK
41+
Connection: keep-alive
42+
Server: gunicorn/19.7.1
43+
Date: Wed, 02 May 2018 20:55:20 GMT
44+
Content-Type: application/json
45+
Access-Control-Allow-Origin: *
46+
Access-Control-Allow-Credentials: true
47+
X-Powered-By: Flask
48+
X-Processed-Time: 0
49+
Content-Length: 220
50+
Via: 1.1 vegur
51+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CURLER_ERROR curl error (22)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"args": {},
3+
"headers": {
4+
"Accept": "*/*",
5+
"Connection": "close",
6+
"Host": "eu.httpbin.org",
7+
"User-Agent": "sinew/1.0.4"
8+
},
9+
"origin": "104.36.46.249",
10+
"url": "http://eu.httpbin.org/get"
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

test/legacy/legacy.sinew

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
get 'http://eu.httpbin.org/status/500'
2+
get 'http://eu.httpbin.org/redirect/3'

test/test_legacy.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require_relative 'test_helper'
2+
3+
class TestLegacy < MiniTest::Test
4+
def setup
5+
super
6+
7+
# These are legacy cache files, pulled from an older version of sinew. We
8+
# use them to test our legacy head parsing.
9+
src = 'legacy/eu.httpbin.org'
10+
dst = "#{TMP}/eu.httpbin.org"
11+
FileUtils.cp_r(File.expand_path(src, __dir__), dst)
12+
end
13+
14+
def test_legacy
15+
sinew.dsl.get('http://eu.httpbin.org/status/500')
16+
assert_equal "\n", sinew.dsl.raw
17+
18+
sinew.dsl.get('http://eu.httpbin.org/redirect/3')
19+
assert_equal 'http://eu.httpbin.org/get', sinew.dsl.url
20+
end
21+
end

0 commit comments

Comments
 (0)