1+ module Stencil
2+ RSpec . describe Client do
3+ let ( :registry_url ) { 'http://stencil.test' }
4+ let ( :bearer_token ) { 'sample-token-123' }
5+ let ( :service_type_message ) do
6+ service_type = nil
7+ Google ::Protobuf ::FileDescriptorSet . decode ( File . read ( 'spec/data/desc-proto-bin' ) ) . file . each do |file |
8+ file . message_type . each do |msg |
9+ if msg . name == "ServiceType"
10+ service_type = msg
11+ end
12+ end
13+ end
14+ service_type
15+ end
16+
17+ context '#get_type' do
18+ subject { Stencil ::Client . new }
19+
20+ before ( :each ) do
21+ Stencil . configure do |config |
22+ config . registry_url = registry_url
23+ config . bearer_token = bearer_token
24+ end
25+
26+ @stencil_get_stub = stub_request ( :get , registry_url ) .
27+ with (
28+ headers : {
29+ 'Authorization' => 'Bearer ' + bearer_token ,
30+ 'Connection' => 'close' ,
31+ 'Host' => 'stencil.test' ,
32+ 'User-Agent' => 'http.rb/4.4.1'
33+ } )
34+ end
35+
36+ it 'should raise error if configs are invalid' do
37+ config = Stencil . configuration
38+ config . bearer_token = ""
39+ expect { subject } . to raise_error ( Stencil ::InvalidConfiguration )
40+ end
41+
42+ it 'should raise error if http client returns error on stencil get api' do
43+ @stencil_get_stub . to_raise ( StandardError . new ( 'some error' ) )
44+ expect { subject . get_type } . to raise_error ( Stencil ::HTTPClientError )
45+ end
46+
47+ it 'should raise error if http client returns 500' do
48+ @stencil_get_stub . to_return ( status : 500 , body : 'Internal server error' , headers : { } )
49+ expect { subject . get_type } . to raise_error ( Stencil ::HTTPClientError )
50+ end
51+
52+ it 'should raise error for invalid proto type' do
53+ @stencil_get_stub . to_return ( status : 200 , body : File . new ( 'spec/data/desc-proto-bin' ) , headers : { } )
54+
55+ proto_name = "incorrect"
56+ expect { subject . get_type ( proto_name ) } . to raise_error ( Stencil ::InvalidProtoClass )
57+ end
58+
59+ it 'should successfully return proto type' do
60+ @stencil_get_stub . to_return ( status : 200 , body : File . new ( 'spec/data/desc-proto-bin' ) , headers : { } )
61+
62+ proto_name = "com.gojek.esb.types.ServiceType"
63+ actual_type = subject . get_type ( proto_name )
64+ expect ( actual_type ) . to eq ( service_type_message )
65+ end
66+ end
67+ end
68+ end
0 commit comments