32
32
NON_DICT_ARGS = ['' , list (), tuple (), True , False , 1 , 0 , {1 : 'foo' }, {'foo' : 1 }]
33
33
NON_OBJECT_ARGS = [list (), tuple (), dict (), 'foo' , 0 , 1 , True , False ]
34
34
NON_LIST_ARGS = ['' , tuple (), dict (), True , False , 1 , 0 , [1 ], ['foo' , 1 ]]
35
- HTTP_ERRORS = [400 , 404 , 500 ] # TODO(hkj): Remove this when IID tests are updated.
36
35
HTTP_ERROR_CODES = {
37
36
400 : exceptions .InvalidArgumentError ,
37
+ 403 : exceptions .PermissionDeniedError ,
38
38
404 : exceptions .NotFoundError ,
39
39
500 : exceptions .InternalError ,
40
40
503 : exceptions .UnavailableError ,
@@ -1859,30 +1859,24 @@ def test_subscribe_to_topic(self, args):
1859
1859
assert recorder [0 ].url == self ._get_url ('iid/v1:batchAdd' )
1860
1860
assert json .loads (recorder [0 ].body .decode ()) == args [2 ]
1861
1861
1862
- @pytest .mark .parametrize ('status' , HTTP_ERRORS )
1863
- def test_subscribe_to_topic_error (self , status ):
1862
+ @pytest .mark .parametrize ('status, exc_type ' , HTTP_ERROR_CODES . items () )
1863
+ def test_subscribe_to_topic_error (self , status , exc_type ):
1864
1864
_ , recorder = self ._instrument_iid_service (
1865
1865
status = status , payload = self ._DEFAULT_ERROR_RESPONSE )
1866
- with pytest .raises (messaging . ApiCallError ) as excinfo :
1866
+ with pytest .raises (exc_type ) as excinfo :
1867
1867
messaging .subscribe_to_topic ('foo' , 'test-topic' )
1868
1868
assert str (excinfo .value ) == 'error_reason'
1869
- code = messaging ._MessagingService .IID_ERROR_CODES .get (
1870
- status , messaging ._MessagingService .UNKNOWN_ERROR )
1871
- assert excinfo .value .code == code
1872
1869
assert len (recorder ) == 1
1873
1870
assert recorder [0 ].method == 'POST'
1874
1871
assert recorder [0 ].url == self ._get_url ('iid/v1:batchAdd' )
1875
1872
1876
- @pytest .mark .parametrize ('status' , HTTP_ERRORS )
1877
- def test_subscribe_to_topic_non_json_error (self , status ):
1873
+ @pytest .mark .parametrize ('status, exc_type ' , HTTP_ERROR_CODES . items () )
1874
+ def test_subscribe_to_topic_non_json_error (self , status , exc_type ):
1878
1875
_ , recorder = self ._instrument_iid_service (status = status , payload = 'not json' )
1879
- with pytest .raises (messaging . ApiCallError ) as excinfo :
1876
+ with pytest .raises (exc_type ) as excinfo :
1880
1877
messaging .subscribe_to_topic ('foo' , 'test-topic' )
1881
1878
reason = 'Unexpected HTTP response with status: {0}; body: not json' .format (status )
1882
- code = messaging ._MessagingService .IID_ERROR_CODES .get (
1883
- status , messaging ._MessagingService .UNKNOWN_ERROR )
1884
1879
assert str (excinfo .value ) == reason
1885
- assert excinfo .value .code == code
1886
1880
assert len (recorder ) == 1
1887
1881
assert recorder [0 ].method == 'POST'
1888
1882
assert recorder [0 ].url == self ._get_url ('iid/v1:batchAdd' )
@@ -1897,30 +1891,24 @@ def test_unsubscribe_from_topic(self, args):
1897
1891
assert recorder [0 ].url == self ._get_url ('iid/v1:batchRemove' )
1898
1892
assert json .loads (recorder [0 ].body .decode ()) == args [2 ]
1899
1893
1900
- @pytest .mark .parametrize ('status' , HTTP_ERRORS )
1901
- def test_unsubscribe_from_topic_error (self , status ):
1894
+ @pytest .mark .parametrize ('status, exc_type ' , HTTP_ERROR_CODES . items () )
1895
+ def test_unsubscribe_from_topic_error (self , status , exc_type ):
1902
1896
_ , recorder = self ._instrument_iid_service (
1903
1897
status = status , payload = self ._DEFAULT_ERROR_RESPONSE )
1904
- with pytest .raises (messaging . ApiCallError ) as excinfo :
1898
+ with pytest .raises (exc_type ) as excinfo :
1905
1899
messaging .unsubscribe_from_topic ('foo' , 'test-topic' )
1906
1900
assert str (excinfo .value ) == 'error_reason'
1907
- code = messaging ._MessagingService .IID_ERROR_CODES .get (
1908
- status , messaging ._MessagingService .UNKNOWN_ERROR )
1909
- assert excinfo .value .code == code
1910
1901
assert len (recorder ) == 1
1911
1902
assert recorder [0 ].method == 'POST'
1912
1903
assert recorder [0 ].url == self ._get_url ('iid/v1:batchRemove' )
1913
1904
1914
- @pytest .mark .parametrize ('status' , HTTP_ERRORS )
1915
- def test_unsubscribe_from_topic_non_json_error (self , status ):
1905
+ @pytest .mark .parametrize ('status, exc_type ' , HTTP_ERROR_CODES . items () )
1906
+ def test_unsubscribe_from_topic_non_json_error (self , status , exc_type ):
1916
1907
_ , recorder = self ._instrument_iid_service (status = status , payload = 'not json' )
1917
- with pytest .raises (messaging . ApiCallError ) as excinfo :
1908
+ with pytest .raises (exc_type ) as excinfo :
1918
1909
messaging .unsubscribe_from_topic ('foo' , 'test-topic' )
1919
1910
reason = 'Unexpected HTTP response with status: {0}; body: not json' .format (status )
1920
- code = messaging ._MessagingService .IID_ERROR_CODES .get (
1921
- status , messaging ._MessagingService .UNKNOWN_ERROR )
1922
1911
assert str (excinfo .value ) == reason
1923
- assert excinfo .value .code == code
1924
1912
assert len (recorder ) == 1
1925
1913
assert recorder [0 ].method == 'POST'
1926
1914
assert recorder [0 ].url == self ._get_url ('iid/v1:batchRemove' )
0 commit comments