@@ -70,6 +70,85 @@ def test_server_responding_with_408_status
7070 end
7171 end
7272
73+ def test_server_responding_with_400_status
74+ stub_request ( :get , "http://example.com/users/1" )
75+ . to_return ( headers : { content_type : "application/vnd.api+json" } , body : {
76+ meta : {
77+ status : 400 ,
78+ message : "Bad Request"
79+ }
80+ } . to_json )
81+
82+ assert_raises JsonApiClient ::Errors ::ClientError do
83+ User . find ( 1 )
84+ end
85+ end
86+
87+ def test_server_responding_with_401_status
88+ stub_request ( :get , "http://example.com/users/1" )
89+ . to_return ( headers : { content_type : "application/vnd.api+json" } , body : {
90+ meta : {
91+ status : 401 ,
92+ message : "Not Authorized"
93+ }
94+ } . to_json )
95+
96+ assert_raises JsonApiClient ::Errors ::NotAuthorized do
97+ User . find ( 1 )
98+ end
99+ end
100+
101+ def test_server_responding_with_400_status_in_meta_with_custom_status_handler
102+ stub_request ( :get , "http://example.com/user_with_custom_status_handlers/1" )
103+ . to_return ( headers : { content_type : "application/vnd.api+json" } , body : {
104+ meta : {
105+ status : 400 ,
106+ message : "Bad Request"
107+ }
108+ } . to_json )
109+
110+ UserWithCustomStatusHandler . find ( 1 )
111+ end
112+
113+ def test_server_responding_with_401_status_in_meta_with_custom_status_handler
114+ stub_request ( :get , "http://example.com/user_with_custom_status_handlers/1" )
115+ . to_return ( headers : { content_type : "application/vnd.api+json" } , body : {
116+ meta : {
117+ status : 401 ,
118+ message : "Not Authorized"
119+ }
120+ } . to_json )
121+
122+ assert_raises CustomUnauthorizedError do
123+ UserWithCustomStatusHandler . find ( 1 )
124+ end
125+ end
126+
127+ def test_server_responding_with_400_status_with_custom_status_handler
128+ stub_request ( :post , "http://example.com/user_with_custom_status_handlers" )
129+ . with ( headers : { content_type : 'application/vnd.api+json' , accept : 'application/vnd.api+json' } , body : {
130+ data : {
131+ type : 'user_with_custom_status_handlers' ,
132+ attributes : {
133+ name : 'foo'
134+ }
135+ }
136+ } . to_json )
137+ . to_return ( status : 400 , headers : { content_type : "application/vnd.api+json" } , body : {
138+ errors : [
139+ {
140+ status : '400' ,
141+ detail : 'Bad Request'
142+ }
143+ ]
144+ } . to_json )
145+
146+ user = UserWithCustomStatusHandler . create ( name : 'foo' )
147+ refute user . persisted?
148+ expected_errors = { base : [ 'Bad Request' ] }
149+ assert_equal expected_errors , user . errors . messages
150+ end
151+
73152 def test_server_responding_with_422_status
74153 stub_request ( :get , "http://example.com/users/1" )
75154 . to_return ( headers : { content_type : "application/vnd.api+json" } , body : {
0 commit comments