webbed 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/.gitignore +22 -0
  2. data/.travis.yml +7 -0
  3. data/.yardopts +6 -0
  4. data/Gemfile +5 -13
  5. data/Guardfile +5 -0
  6. data/README.md +36 -31
  7. data/Rakefile +7 -14
  8. data/lib/webbed.rb +28 -6
  9. data/lib/webbed/generic_message.rb +27 -15
  10. data/lib/webbed/headers.rb +2 -0
  11. data/lib/webbed/helpers/entity_headers_helper.rb +62 -0
  12. data/lib/webbed/helpers/method_helper.rb +83 -0
  13. data/lib/webbed/helpers/rack_request_helper.rb +86 -0
  14. data/lib/webbed/helpers/rack_response_helper.rb +56 -0
  15. data/lib/webbed/helpers/request_headers_helper.rb +62 -0
  16. data/lib/webbed/helpers/request_uri_helper.rb +34 -0
  17. data/lib/webbed/helpers/response_headers_helper.rb +48 -0
  18. data/lib/webbed/helpers/scheme_helper.rb +26 -0
  19. data/lib/webbed/http_version.rb +88 -33
  20. data/lib/webbed/media_type.rb +160 -0
  21. data/lib/webbed/method.rb +63 -33
  22. data/lib/webbed/request.rb +65 -21
  23. data/lib/webbed/response.rb +65 -24
  24. data/lib/webbed/status_code.rb +84 -17
  25. data/lib/webbed/version.rb +1 -1
  26. data/test/support/assertions.rb +17 -0
  27. data/test/support/runner.rb +326 -0
  28. data/test/test_helper.rb +13 -0
  29. data/test/webbed/generic_message_test.rb +44 -0
  30. data/test/webbed/headers_test.rb +31 -0
  31. data/test/webbed/helpers/entity_headers_helper_test.rb +68 -0
  32. data/test/webbed/helpers/method_helper_test.rb +151 -0
  33. data/test/webbed/helpers/rack_request_helper_test.rb +108 -0
  34. data/test/webbed/helpers/rack_response_helper_test.rb +33 -0
  35. data/test/webbed/helpers/request_headers_helper_test.rb +57 -0
  36. data/test/webbed/helpers/request_uri_helper_test.rb +32 -0
  37. data/test/webbed/helpers/response_headers_helper_test.rb +46 -0
  38. data/test/webbed/helpers/scheme_helper_test.rb +28 -0
  39. data/test/webbed/http_version_test.rb +52 -0
  40. data/test/webbed/media_type_test.rb +100 -0
  41. data/test/webbed/method_test.rb +160 -0
  42. data/test/webbed/request_test.rb +74 -0
  43. data/test/webbed/response_test.rb +86 -0
  44. data/test/webbed/status_code_test.rb +105 -0
  45. data/webbed.gemspec +31 -0
  46. metadata +128 -41
@@ -0,0 +1,52 @@
1
+ require 'test_helper'
2
+
3
+ module WebbedTest
4
+ class HTTPVersionTest < TestCase
5
+ test '#initialize with string' do
6
+ version = Webbed::HTTPVersion.new('HTTP/1.1')
7
+ assert_equal 1, version.major
8
+ assert_equal 1, version.minor
9
+ end
10
+
11
+ test '#initialize with float' do
12
+ version = Webbed::HTTPVersion.new(2.0)
13
+ assert_equal 2, version.major
14
+ assert_equal 0, version.minor
15
+ end
16
+
17
+ test '#to_s' do
18
+ assert_equal 'HTTP/2.0', Webbed::HTTPVersion.new('HTTP/2.0').to_s
19
+ assert_equal 'HTTP/2.0', Webbed::HTTPVersion.new(2.0).to_s
20
+ end
21
+
22
+ test '#to_f' do
23
+ version = Webbed::HTTPVersion.new('HTTP/2.0')
24
+ assert_equal 2.0, version.to_f
25
+
26
+ version = Webbed::HTTPVersion.new(2.0)
27
+ assert_equal 2.0, version.to_f
28
+ end
29
+
30
+ test 'comparisons' do
31
+ one_point_one = Webbed::HTTPVersion.new('HTTP/1.1')
32
+ two_point_oh1 = Webbed::HTTPVersion.new('HTTP/2.0')
33
+ two_point_oh2 = Webbed::HTTPVersion.new('HTTP/2.0')
34
+
35
+ assert_equal 1.1, one_point_one
36
+ refute_equal 1.0, one_point_one
37
+ assert_equal two_point_oh1, two_point_oh2
38
+
39
+ assert_comparable one_point_one, two_point_oh1
40
+ end
41
+
42
+ test 'ONE_POINT_ONE' do
43
+ assert_equal 1, Webbed::HTTPVersion::ONE_POINT_ONE.major
44
+ assert_equal 1, Webbed::HTTPVersion::ONE_POINT_ONE.minor
45
+ end
46
+
47
+ test 'ONE_POINT_OH' do
48
+ assert_equal 1, Webbed::HTTPVersion::ONE_POINT_OH.major
49
+ assert_equal 0, Webbed::HTTPVersion::ONE_POINT_OH.minor
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,100 @@
1
+ require 'test_helper'
2
+
3
+ module WebbedTest
4
+ class MediaTypeTest < TestCase
5
+ test '#initialize with only MIME type' do
6
+ media_type = Webbed::MediaType.new('text/html')
7
+
8
+ assert_equal 'text', media_type.type
9
+ assert_equal 'html', media_type.subtype
10
+ assert_equal 'text/html', media_type.mime_type
11
+ assert media_type.parameters.empty?
12
+ end
13
+
14
+ test '#initialize with MIME type and parameters' do
15
+ media_type = Webbed::MediaType.new('text/html; foo=bar; lol=rofl')
16
+
17
+ assert_equal 'text', media_type.type
18
+ assert_equal 'html', media_type.subtype
19
+ assert_equal 'text/html', media_type.mime_type
20
+ assert_equal 'bar', media_type.parameters['foo']
21
+ assert_equal 'rofl', media_type.parameters['lol']
22
+ end
23
+
24
+ test '#initialize with MIME type and weirdly spaced parameters' do
25
+ media_type = Webbed::MediaType.new('text/html; foo=bar ; lol=rofl')
26
+
27
+ assert_equal 'text', media_type.type
28
+ assert_equal 'html', media_type.subtype
29
+ assert_equal 'text/html', media_type.mime_type
30
+ assert_equal 'bar', media_type.parameters['foo']
31
+ assert_equal 'rofl', media_type.parameters['lol']
32
+ end
33
+
34
+ test '#mime_type' do
35
+ media_type = Webbed::MediaType.new('text/html; foo=bar; lol=rofl')
36
+ media_type.mime_type = 'application/json'
37
+
38
+ assert_equal 'application', media_type.type
39
+ assert_equal 'json', media_type.subtype
40
+ assert_equal 'application/json', media_type.mime_type
41
+ end
42
+
43
+ test '#type' do
44
+ media_type = Webbed::MediaType.new('text/html; foo=bar; lol=rofl')
45
+ media_type.type = 'image'
46
+
47
+ assert_equal 'image', media_type.type
48
+ assert_equal 'html', media_type.subtype
49
+ assert_equal 'image/html', media_type.mime_type
50
+ end
51
+
52
+ test '#subtype' do
53
+ media_type = Webbed::MediaType.new('text/html; foo=bar; lol=rofl')
54
+ media_type.subtype = 'xml'
55
+
56
+ assert_equal 'text', media_type.type
57
+ assert_equal 'xml', media_type.subtype
58
+ assert_equal 'text/xml', media_type.mime_type
59
+ end
60
+
61
+ test '#parameters' do
62
+ media_type = Webbed::MediaType.new('text/html; foo=bar; lol=rofl')
63
+ media_type.parameters = { 'test' => 'lol' }
64
+
65
+ assert_equal 'lol', media_type.parameters['test']
66
+ end
67
+
68
+ test '#to_s' do
69
+ assert_equal 'text/html', Webbed::MediaType.new('text/html').to_s
70
+
71
+ media_type_string = Webbed::MediaType.new('text/html; foo=bar; lol=rofl').to_s
72
+ assert media_type_string.include?('; lol=rofl')
73
+ assert media_type_string.include?('; foo=bar')
74
+ assert media_type_string.include?('text/html')
75
+ end
76
+
77
+ test '#vendor_specific?' do
78
+ refute Webbed::MediaType.new('text/html; foo=bar; lol=rofl').vendor_specific?
79
+ assert Webbed::MediaType.new('application/vnd.lol+xml').vendor_specific?
80
+ end
81
+
82
+ test '#suffix' do
83
+ suffixed = Webbed::MediaType.new('application/atom+xml')
84
+ assert_equal 'xml', suffixed.suffix
85
+ assert_includes suffixed.interpretable_as, 'application/xml'
86
+ assert_includes suffixed.interpretable_as, 'application/atom+xml'
87
+
88
+ not_suffixed = Webbed::MediaType.new('text/html')
89
+ assert_nil not_suffixed.suffix
90
+ assert_includes not_suffixed.interpretable_as, 'text/html'
91
+ end
92
+
93
+ test 'equality' do
94
+ json = Webbed::MediaType.new('application/json')
95
+
96
+ assert_equal Webbed::MediaType.new('application/json'), json
97
+ refute_equal Webbed::MediaType.new('text/html'), json
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,160 @@
1
+ require 'test_helper'
2
+
3
+ module WebbedTest
4
+ class MethodTest < TestCase
5
+ test '#initialize without options' do
6
+ fake = Webbed::Method.new('FAKE')
7
+
8
+ assert_equal 'FAKE', fake.to_s
9
+ refute fake.safe?
10
+ refute fake.idempotent?
11
+ assert_includes fake.allowable_entities, :request
12
+ assert_includes fake.allowable_entities, :response
13
+ end
14
+
15
+ test '#initialize a safe Method' do
16
+ fake = Webbed::Method.new('FAKE', :safe => true)
17
+ assert fake.safe?
18
+ assert fake.idempotent?
19
+ end
20
+
21
+ test '#initialize an idempotent Method' do
22
+ fake = Webbed::Method.new('FAKE', :idempotent => true)
23
+ refute fake.safe?
24
+ assert fake.idempotent?
25
+ end
26
+
27
+ test '#initialize an unsafe Method' do
28
+ fake = Webbed::Method.new('FAKE', :safe => false)
29
+ refute fake.safe?
30
+ refute fake.idempotent?
31
+ end
32
+
33
+ test '#initialize a headers-only Method' do
34
+ fake = Webbed::Method.new('FAKE', :allowable_entities => [])
35
+ refute_includes fake.allowable_entities, :request
36
+ refute_includes fake.allowable_entities, :response
37
+ end
38
+
39
+ test '#initialize a response-entity-only Method' do
40
+ fake = Webbed::Method.new('FAKE', :allowable_entities => [:response])
41
+ refute_includes fake.allowable_entities, :request
42
+ assert_includes fake.allowable_entities, :response
43
+ end
44
+
45
+ test '#initialize a two-entity Method' do
46
+ fake = Webbed::Method.new('FAKE', :allowable_entities => [:request, :response])
47
+ assert_includes fake.allowable_entities, :request
48
+ assert_includes fake.allowable_entities, :response
49
+ end
50
+
51
+ test 'cached Methods' do
52
+ assert_same Webbed::Method::OPTIONS, Webbed::Method.new('OPTIONS')
53
+ assert_same Webbed::Method::GET, Webbed::Method.new('GET')
54
+ assert_same Webbed::Method::HEAD, Webbed::Method.new('HEAD')
55
+ assert_same Webbed::Method::POST, Webbed::Method.new('POST')
56
+ assert_same Webbed::Method::PUT, Webbed::Method.new('PUT')
57
+ assert_same Webbed::Method::DELETE, Webbed::Method.new('DELETE')
58
+ assert_same Webbed::Method::TRACE, Webbed::Method.new('TRACE')
59
+ assert_same Webbed::Method::CONNECT, Webbed::Method.new('CONNECT')
60
+ assert_same Webbed::Method::PATCH, Webbed::Method.new('PATCH')
61
+ end
62
+
63
+ test 'equality' do
64
+ fake = Webbed::Method.new('FAKE')
65
+
66
+ assert_equal Webbed::Method.new('FAKE'), fake
67
+ refute_equal Webbed::Method::POST, fake
68
+ end
69
+
70
+ test 'OPTIONS' do
71
+ method = Webbed::Method::OPTIONS
72
+
73
+ assert_equal 'OPTIONS', method.to_s
74
+ assert method.safe?
75
+ assert method.idempotent?
76
+ refute_includes method.allowable_entities, :request
77
+ assert_includes method.allowable_entities, :response
78
+ end
79
+
80
+ test 'GET' do
81
+ method = Webbed::Method::GET
82
+
83
+ assert_equal 'GET', method.to_s
84
+ assert method.safe?
85
+ assert method.idempotent?
86
+ refute_includes method.allowable_entities, :request
87
+ assert_includes method.allowable_entities, :response
88
+ end
89
+
90
+ test 'HEAD' do
91
+ method = Webbed::Method::HEAD
92
+
93
+ assert_equal 'HEAD', method.to_s
94
+ assert method.safe?
95
+ assert method.idempotent?
96
+ refute_includes method.allowable_entities, :request
97
+ refute_includes method.allowable_entities, :response
98
+ end
99
+
100
+ test 'POST' do
101
+ method = Webbed::Method::POST
102
+
103
+ assert_equal 'POST', method.to_s
104
+ refute method.safe?
105
+ refute method.idempotent?
106
+ assert_includes method.allowable_entities, :request
107
+ assert_includes method.allowable_entities, :response
108
+ end
109
+
110
+ test 'PUT' do
111
+ method = Webbed::Method::PUT
112
+
113
+ assert_equal 'PUT', method.to_s
114
+ refute method.safe?
115
+ assert method.idempotent?
116
+ assert_includes method.allowable_entities, :request
117
+ assert_includes method.allowable_entities, :response
118
+ end
119
+
120
+ test 'DELETE' do
121
+ method = Webbed::Method::DELETE
122
+
123
+ assert_equal 'DELETE', method.to_s
124
+ refute method.safe?
125
+ assert method.idempotent?
126
+ refute_includes method.allowable_entities, :request
127
+ assert_includes method.allowable_entities, :response
128
+ end
129
+
130
+ test 'TRACE' do
131
+ method = Webbed::Method::TRACE
132
+
133
+ assert_equal 'TRACE', method.to_s
134
+ assert method.safe?
135
+ assert method.idempotent?
136
+ refute_includes method.allowable_entities, :request
137
+ assert_includes method.allowable_entities, :response
138
+ end
139
+
140
+ test 'CONNECT' do
141
+ method = Webbed::Method::CONNECT
142
+
143
+ assert_equal 'CONNECT', method.to_s
144
+ refute method.safe?
145
+ refute method.idempotent?
146
+ assert_includes method.allowable_entities, :request
147
+ assert_includes method.allowable_entities, :response
148
+ end
149
+
150
+ test 'PATCH' do
151
+ method = Webbed::Method::PATCH
152
+
153
+ assert_equal 'PATCH', method.to_s
154
+ refute method.safe?
155
+ refute method.idempotent?
156
+ assert_includes method.allowable_entities, :request
157
+ assert_includes method.allowable_entities, :response
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,74 @@
1
+ require 'test_helper'
2
+
3
+ module WebbedTest
4
+ class RequestTest < TestCase
5
+ test '#initialize without options' do
6
+ request = Webbed::Request.new(
7
+ 'POST',
8
+ 'http://google.com',
9
+ { 'Content-Type' => 'text/plain' },
10
+ 'Test 1 2 3'
11
+ )
12
+
13
+ assert_equal Webbed::Method::POST, request.method
14
+ assert_instance_of Addressable::URI, request.request_uri
15
+ assert_equal 'http://google.com', request.request_uri.to_s
16
+ assert_equal 'text/plain', request.headers['Content-Type']
17
+ assert_equal 'Test 1 2 3', request.entity_body
18
+ assert_equal Webbed::HTTPVersion::ONE_POINT_ONE, request.http_version
19
+ assert_equal 'http', request.scheme
20
+ end
21
+
22
+ test '#initialize with an HTTP Version' do
23
+ request = Webbed::Request.new(
24
+ 'POST',
25
+ 'http://google.com',
26
+ { 'Content-Type' => 'text/plain' },
27
+ 'Test 1 2 3',
28
+ :http_version => 1.0
29
+ )
30
+
31
+ assert_equal Webbed::HTTPVersion::ONE_POINT_OH, request.http_version
32
+ end
33
+
34
+ test '#initialize with a scheme' do
35
+ request = Webbed::Request.new(
36
+ 'POST',
37
+ 'http://google.com',
38
+ { 'Content-Type' => 'text/plain' },
39
+ 'Test 1 2 3',
40
+ :scheme => 'https'
41
+ )
42
+
43
+ assert_equal 'https', request.scheme
44
+ end
45
+
46
+ test '#method' do
47
+ request = Webbed::Request.new(
48
+ 'POST',
49
+ 'http://google.com',
50
+ { 'Content-Type' => 'text/plain' },
51
+ 'Test 1 2 3'
52
+ )
53
+
54
+ request.method = 'GET'
55
+
56
+ assert_instance_of Webbed::Method, request.method
57
+ assert_equal Webbed::Method::GET, request.method
58
+
59
+ assert_instance_of Method, request.method(:__send__)
60
+ end
61
+
62
+ test '#scheme' do
63
+ request = Webbed::Request.new(
64
+ 'POST',
65
+ 'http://google.com',
66
+ { 'Content-Type' => 'text/plain' },
67
+ 'Test 1 2 3'
68
+ )
69
+
70
+ request.scheme = 'https'
71
+ assert_equal 'https', request.scheme
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,86 @@
1
+ require 'test_helper'
2
+
3
+ module WebbedTest
4
+ class ResponseTest < TestCase
5
+ test '#initialize without options' do
6
+ response = Webbed::Response.new(
7
+ 404,
8
+ { 'Content-Type' => 'text/plain' },
9
+ 'Test 1 2 3'
10
+ )
11
+
12
+ assert_equal 'text/plain', response.headers['Content-Type']
13
+ assert_equal 'Test 1 2 3', response.entity_body
14
+ assert_equal Webbed::HTTPVersion::ONE_POINT_ONE, response.http_version
15
+ end
16
+
17
+ test '#initialize with a Reason Phrase' do
18
+ response = Webbed::Response.new(
19
+ '200 LOLOL',
20
+ {},
21
+ 'Test 1 2 3'
22
+ )
23
+
24
+ assert_equal 'LOLOL', response.reason_phrase
25
+
26
+ assert_instance_of Webbed::StatusCode, response.status_code
27
+ assert_equal 200, response.status_code
28
+ end
29
+
30
+ test '#initialize without a Reason Phrase' do
31
+ response = Webbed::Response.new(
32
+ 404,
33
+ {},
34
+ 'Test 1 2 3'
35
+ )
36
+
37
+ assert_equal 'Not Found', response.reason_phrase
38
+
39
+ assert_instance_of Webbed::StatusCode, response.status_code
40
+ assert_equal 404, response.status_code
41
+ end
42
+
43
+ test '#initialize with an HTTP Version' do
44
+ response = Webbed::Response.new(
45
+ 404,
46
+ {},
47
+ 'Test 1 2 3',
48
+ :http_version => 1.0
49
+ )
50
+
51
+ assert_equal Webbed::HTTPVersion::ONE_POINT_OH, response.http_version
52
+ end
53
+
54
+ test '#status_code' do
55
+ response = Webbed::Response.new(
56
+ 404,
57
+ {},
58
+ 'Test 1 2 3'
59
+ )
60
+
61
+ assert_instance_of Webbed::StatusCode, response.status_code
62
+ assert_equal 404, response.status_code
63
+
64
+ response.status_code = 100
65
+
66
+ assert_instance_of Webbed::StatusCode, response.status_code
67
+ assert_equal 100, response.status_code
68
+ end
69
+
70
+ test '#reason_phrase' do
71
+ response = Webbed::Response.new(
72
+ 500,
73
+ {},
74
+ 'Test 1 2 3'
75
+ )
76
+
77
+ assert_equal 'Internal Server Error', response.reason_phrase
78
+
79
+ response.status_code = 404
80
+ assert_equal 'Not Found', response.reason_phrase
81
+
82
+ response.reason_phrase = 'FOOBAR'
83
+ assert_equal 'FOOBAR', response.reason_phrase
84
+ end
85
+ end
86
+ end