/
AuthenticationTestBase.java
102 lines (91 loc) · 3.8 KB
/
AuthenticationTestBase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* Copyright (c) 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.api.client.auth.oauth2;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.LowLevelHttpRequest;
import com.google.api.client.http.LowLevelHttpResponse;
import com.google.api.client.json.Json;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.testing.http.MockHttpTransport;
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
import com.google.api.client.testing.http.MockLowLevelHttpResponse;
import com.google.api.client.util.GenericData;
import java.io.IOException;
import junit.framework.TestCase;
/**
* Base class for tests of authentication code.
*
* @author Yaniv Inbar
*/
public abstract class AuthenticationTestBase extends TestCase {
protected static final JsonFactory JSON_FACTORY = new GsonFactory();
protected static final String ACCESS_TOKEN = "abc";
protected static final String NEW_ACCESS_TOKEN = "def";
protected static final GenericUrl TOKEN_SERVER_URL = new GenericUrl("http://example.com/token");
protected static final String CLIENT_ID = "id";
protected static final String CLIENT_SECRET = "secret";
protected static final String REFRESH_TOKEN = "refreshToken";
protected static final String NEW_REFRESH_TOKEN = "newRefreshToken";
protected static final long EXPIRES_IN = 3600;
/** Mock transport class which has been extended to enable more precise testing. */
protected static class AccessTokenTransport extends MockHttpTransport {
int statusCode = 200;
String wwwAuthenticate = null;
int calls = 0;
@Override
public LowLevelHttpRequest buildRequest(String method, String url) {
return new MockLowLevelHttpRequest(url) {
@Override
public LowLevelHttpResponse execute() throws IOException {
calls++;
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
response.setContentType(Json.MEDIA_TYPE);
GenericData responseData;
if (statusCode == 401 || wwwAuthenticate != null) {
// return 401 or invalid_token error (with the given status code), and then reset
// wwwAuthenticate and statusCode - so next request to refresh the token will succeed
if (wwwAuthenticate != null) {
response.addHeader("WWW-Authenticate", wwwAuthenticate);
wwwAuthenticate = null;
}
response.setStatusCode(statusCode);
statusCode = 200;
return response;
}
if (statusCode != 200) {
TokenErrorResponse json = new TokenErrorResponse();
json.setError("invalid_client");
responseData = json;
response.setStatusCode(statusCode);
} else {
TokenResponse json = new TokenResponse();
json.setAccessToken(NEW_ACCESS_TOKEN);
json.setRefreshToken(NEW_REFRESH_TOKEN);
json.setExpiresInSeconds(EXPIRES_IN);
responseData = json;
}
response.setContent(JSON_FACTORY.toString(responseData));
return response;
}
};
}
}
public AuthenticationTestBase() {
super();
}
public AuthenticationTestBase(String name) {
super(name);
}
}