3333import com .google .api .client .testing .http .MockLowLevelHttpResponse ;
3434import com .google .protobuf .InvalidProtocolBufferException ;
3535import com .google .protobuf .util .JsonFormat ;
36-
37- import org .junit .Before ;
38- import org .junit .Test ;
39-
4036import java .io .IOException ;
4137import java .util .LinkedList ;
38+ import org .junit .Before ;
39+ import org .junit .Test ;
4240
4341/**
4442 * Tests for {@link ServiceConfigSupplier}.
4543 */
4644public final class ServiceConfigSupplierTest {
47-
4845 private static final String SERVICE_NAME = "test-service-name" ;
4946 private static final String SERVICE_VERSION = "test-service-version" ;
5047
51- private static final Service SERVICE = Service .newBuilder ()
52- .setName (SERVICE_NAME )
53- .setId (SERVICE_VERSION )
54- .build ();
48+ private static final Service SERVICE =
49+ Service .newBuilder ().setName (SERVICE_NAME ).setId (SERVICE_VERSION ).build ();
5550
5651 private final Environment mockEnvironment = mock (Environment .class );
5752 private final GoogleCredential mockCredential = mock (GoogleCredential .class );
@@ -72,8 +67,7 @@ public void testServiceNameNotSet() throws IOException {
7267 fail ();
7368 } catch (IllegalArgumentException exception ) {
7469 assertEquals (
75- "Environment variable 'ENDPOINTS_SERVICE_NAME' is not set" ,
76- exception .getMessage ());
70+ "Environment variable 'ENDPOINTS_SERVICE_NAME' is not set" , exception .getMessage ());
7771 }
7872 }
7973
@@ -94,13 +88,24 @@ public void testServiceVersionNotSet() throws IOException {
9488 public void testFetchSuccessfully () throws InvalidProtocolBufferException {
9589 when (mockEnvironment .getVariable ("ENDPOINTS_SERVICE_NAME" )).thenReturn (SERVICE_NAME );
9690 when (mockEnvironment .getVariable ("ENDPOINTS_SERVICE_VERSION" )).thenReturn (SERVICE_VERSION );
97-
9891 String content = JsonFormat .printer ().print (SERVICE );
9992 testHttpTransport .addResponse (200 , content );
10093
10194 assertEquals (SERVICE , fetcher .get ());
10295 }
10396
97+ @ Test
98+ public void testFetchWithUnkownFields () throws InvalidProtocolBufferException {
99+ when (mockEnvironment .getVariable ("ENDPOINTS_SERVICE_NAME" )).thenReturn (SERVICE_NAME );
100+ when (mockEnvironment .getVariable ("ENDPOINTS_SERVICE_VERSION" )).thenReturn (SERVICE_VERSION );
101+
102+ String unknownField = ",\" unknown_key\" :\" unknown_value\" \n " ;
103+ String contentBase = JsonFormat .printer ().print (SERVICE );
104+ String content = contentBase .substring (0 ,contentBase .length ()-1 ) + unknownField + contentBase .charAt (contentBase .length ()-1 );
105+ testHttpTransport .addResponse (200 , content );
106+ assertEquals (SERVICE , fetcher .get ());
107+ }
108+
104109 @ Test
105110 public void testFetchConfigWithWrongServiceName () throws InvalidProtocolBufferException {
106111 when (mockEnvironment .getVariable ("ENDPOINTS_SERVICE_NAME" )).thenReturn (SERVICE_NAME );
@@ -124,10 +129,7 @@ public void testFetchConfigWithWrongServiceVersion() throws InvalidProtocolBuffe
124129 when (mockEnvironment .getVariable ("ENDPOINTS_SERVICE_NAME" )).thenReturn (SERVICE_NAME );
125130 when (mockEnvironment .getVariable ("ENDPOINTS_SERVICE_VERSION" )).thenReturn (SERVICE_VERSION );
126131
127- Service service = Service .newBuilder ()
128- .setName (SERVICE_NAME )
129- .setId ("random-version" )
130- .build ();
132+ Service service = Service .newBuilder ().setName (SERVICE_NAME ).setId ("random-version" ).build ();
131133 String content = JsonFormat .printer ().print (service );
132134 testHttpTransport .addResponse (200 , content );
133135
0 commit comments