Support mocking HTTP requests out for testing. Essentially you register URLs to mock, and the URL to direct to instead. (Generally this would be a local HTTP server.) It can't be mocked to a file URL because we cast to HttpURLConnection in places, but it can be built to not hit eBay.
- A jbidwatcher/trunk/src/com/jbidwatcher/util/http/HttpInterface.java
- A jbidwatcher/trunk/src/com/jbidwatcher/util/http/HttpMock.java
- M jbidwatcher/trunk/src/com/jbidwatcher/my/MyJBidwatcher.java view
- M jbidwatcher/trunk/src/com/jbidwatcher/util/http/CookieJar.java view
- M jbidwatcher/trunk/src/com/jbidwatcher/util/http/Http.java view
| 1027 | 1032 | |
|---|---|---|
| 15 | 15 | import com.jbidwatcher.util.xml.XMLInterface; |
| 16 | 16 | import com.jbidwatcher.util.http.Http; |
| 17 | 17 | import com.jbidwatcher.util.http.ClientHttpRequest; |
| 18 | import com.jbidwatcher.util.http.HttpInterface; | |
| 18 | 19 | import com.jbidwatcher.auction.AuctionEntry; |
| 19 | 20 | import com.jbidwatcher.auction.EntryCorral; |
| 20 | 21 | |
| --- | --- | |
| 35 | 36 | */ |
| 36 | 37 | public class MyJBidwatcher { |
| 37 | 38 | private static MyJBidwatcher sInstance = null; |
| 38 | private Http mNet = null; | |
| 39 | private HttpInterface mNet = null; | |
| 39 | 40 | private static String LOG_UPLOAD_URL = "my.jbidwatcher.com/upload/log"; |
| 40 | 41 | private static String ITEM_UPLOAD_URL = "my.jbidwatcher.com/upload/listing"; |
| 41 | 42 | private static String SYNC_UPLOAD_URL = "my.jbidwatcher.com/upload/sync"; |
| --- | --- | |
| 53 | 54 | return "http://" + url; |
| 54 | 55 | } |
| 55 | 56 | |
| 56 | private Http http() { | |
| 57 | private HttpInterface http() { | |
| 57 | 58 | if(mNet == null) { |
| 58 | 59 | mNet = new Http(); |
| 59 | 60 | } |
| 996 | 1032 | |
|---|---|---|
| 12 | 12 | import java.io.IOException; |
| 13 | 13 | |
| 14 | 14 | public class CookieJar { |
| 15 | private Map<String, Cookie> _cookies; | |
| 16 | private boolean m_ignore_redirect_cookies = false; | |
| 17 | private final static boolean do_uber_debug = false; | |
| 15 | private Map<String, Cookie> mCookies; | |
| 16 | private boolean mIgnoreRedirectCookies = false; | |
| 17 | private final static boolean sUberDebug = false; | |
| 18 | 18 | |
| 19 | 19 | public CookieJar() { |
| 20 | _cookies = new TreeMap<String, Cookie>(); | |
| 20 | mCookies = new TreeMap<String, Cookie>(); | |
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | public Cookie getCookie(String keyName) { |
| 24 | return _cookies.get(keyName); | |
| 24 | return mCookies.get(keyName); | |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | public String dump() { |
| 28 | 28 | StringBuffer rval = new StringBuffer(); |
| 29 | for(String cookieName : _cookies.keySet()) { | |
| 30 | Cookie value = _cookies.get(cookieName); | |
| 29 | for(String cookieName : mCookies.keySet()) { | |
| 30 | Cookie value = mCookies.get(cookieName); | |
| 31 | 31 | rval.append(cookieName).append(": ").append(value.getValue()).append('\n'); |
| 32 | 32 | } |
| 33 | 33 | return rval.toString(); |
| --- | --- | |
| 163 | 163 | do { |
| 164 | 164 | nextKey = uc.getHeaderFieldKey(i); |
| 165 | 165 | if(nextKey != null) { |
| 166 | if(do_uber_debug) { | |
| 166 | if(sUberDebug) { | |
| 167 | 167 | JConfig.log().logDebug(nextKey+": " + uc.getHeaderField(i)); |
| 168 | 168 | } |
| 169 | 169 | // If we're redirected, shortcut to loading the new page. |
| --- | --- | |
| 179 | 179 | if(nextKey.startsWith("Set-Cookie") || |
| 180 | 180 | nextKey.startsWith("Set-cookie")) { |
| 181 | 181 | Cookie newCookie = new Cookie(uc.getHeaderField(i)); |
| 182 | _cookies.put(newCookie.getKey(), newCookie); | |
| 182 | mCookies.put(newCookie.getKey(), newCookie); | |
| 183 | 183 | } |
| 184 | 184 | } |
| 185 | 185 | i++; |
| --- | --- | |
| 215 | 215 | } |
| 216 | 216 | |
| 217 | 217 | private HttpURLConnection initiateRequest(boolean post, String sendRequest, String cgi, String referer) { |
| 218 | HttpURLConnection uc; | |
| 218 | URLConnection uc; | |
| 219 | String cookies = mCookies.isEmpty() ? null : this.toString(); | |
| 219 | 220 | |
| 220 | if(!_cookies.isEmpty()) { | |
| 221 | if(post) { | |
| 222 | uc = (HttpURLConnection)Http.net().postFormPage(sendRequest, cgi, this.toString(), referer, m_ignore_redirect_cookies); | |
| 223 | } else { | |
| 224 | uc = (HttpURLConnection)Http.net().getPage(sendRequest, this.toString(), referer, m_ignore_redirect_cookies); | |
| 225 | } | |
| 221 | if(post) { | |
| 222 | uc = Http.net().postFormPage(sendRequest, cgi, cookies, referer, mIgnoreRedirectCookies); | |
| 226 | 223 | } else { |
| 227 | if(post) { | |
| 228 | uc = (HttpURLConnection)Http.net().postFormPage(sendRequest, cgi, null, referer, m_ignore_redirect_cookies); | |
| 229 | } else { | |
| 230 | uc = (HttpURLConnection)Http.net().getPage(sendRequest, null, referer, m_ignore_redirect_cookies); | |
| 231 | } | |
| 224 | uc = Http.net().getPage(sendRequest, cookies, referer, mIgnoreRedirectCookies); | |
| 232 | 225 | } |
| 233 | return uc; | |
| 226 | ||
| 227 | return (HttpURLConnection)uc; | |
| 234 | 228 | } |
| 235 | 229 | |
| 236 | 230 | public String toString() { |
| 237 | 231 | boolean firstThrough = true; |
| 238 | 232 | StringBuffer outBuf = null; |
| 239 | 233 | |
| 240 | for (Cookie cookie : _cookies.values()) { | |
| 234 | for (Cookie cookie : mCookies.values()) { | |
| 241 | 235 | if (cookie.getValue().length() != 0) { |
| 242 | 236 | if (!firstThrough) { |
| 243 | 237 | outBuf.append("; "); |
| 964 | 1032 | |
|---|---|---|
| 16 | 16 | import java.io.*; |
| 17 | 17 | import java.util.Map; |
| 18 | 18 | |
| 19 | public class Http { | |
| 19 | public class Http implements HttpInterface { | |
| 20 | 20 | private String mUsername = null; |
| 21 | 21 | private String mPassword = null; |
| 22 | 22 | |
| 23 | private static Http sInstance = new Http(); | |
| 24 | public static Http net() { return sInstance; } | |
| 23 | private static HttpInterface sInstance = new Http(); | |
| 24 | public static HttpInterface net() { return sInstance; } | |
| 25 | 25 | |
| 26 | 26 | public void setAuthInfo(String user, String pass) { |
| 27 | 27 | mUsername = user; |
| --- | --- | |
| 54 | 54 | } |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | public URLConnection postFormPage(String urlToPost, String cgiData, String cookie, String referer, boolean follow_redirects) { | |
| 57 | public URLConnection postFormPage(String url, String cgiData, String cookie, String referer, boolean followRedirects) { | |
| 58 | 58 | URLConnection huc; |
| 59 | 59 | |
| 60 | 60 | try { |
| 61 | 61 | if(JConfig.queryConfiguration("debug.urls", "false").equals("true")) { |
| 62 | JConfig.log().logDebug("postFormPage: " + urlToPost); | |
| 62 | JConfig.log().logDebug("postFormPage: " + url); | |
| 63 | 63 | } |
| 64 | URL authURL = new URL(urlToPost); | |
| 64 | URL authURL = new URL(url); | |
| 65 | 65 | |
| 66 | 66 | huc = authURL.openConnection(); |
| 67 | 67 | setConnectionInfo(huc); |
| --- | --- | |
| 70 | 70 | if(huc instanceof HttpURLConnection) { |
| 71 | 71 | HttpURLConnection conn = (HttpURLConnection)huc; |
| 72 | 72 | conn.setRequestMethod("POST"); |
| 73 | if(!follow_redirects) conn.setInstanceFollowRedirects(false); | |
| 73 | if(!followRedirects) conn.setInstanceFollowRedirects(false); | |
| 74 | 74 | } |
| 75 | 75 | if(JConfig.queryConfiguration("debug.uber", "false").equals("true") && JConfig.debugging) { |
| 76 | 76 | dumpFormHeaders(System.err, cgiData, cookie); |
| --- | --- | |
| 124 | 124 | return uc; |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | public ByteBuffer getURL(URL dataURL) { | |
| 128 | return getURL(dataURL, null); | |
| 127 | public ByteBuffer getURL(URL url) { | |
| 128 | return getURL(url, null); | |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | /** |
| 132 | 132 | * @brief Retrieve data from HTTP in raw byte form. |
| 133 | 133 | * |
| 134 | * @param dataURL - The URL of the raw data to retrieve. | |
| 134 | * @param url - The URL of the raw data to retrieve. | |
| 135 | 135 | * @param inCookie - Any cookie needed to be passed along. |
| 136 | 136 | * |
| 137 | 137 | * @return - A result with raw data and the length. |
| 138 | 138 | */ |
| 139 | private ByteBuffer getURL(URL dataURL, String inCookie) { | |
| 139 | private ByteBuffer getURL(URL url, String inCookie) { | |
| 140 | 140 | ByteBuffer rval; |
| 141 | 141 | |
| 142 | 142 | try { |
| 143 | rval = receiveData(makeRequest(dataURL, inCookie)); | |
| 143 | rval = receiveData(makeRequest(url, inCookie)); | |
| 144 | 144 | } catch(FileNotFoundException fnfe) { |
| 145 | 145 | // It'd be great if we could pass along something that said, 'not here, never will be'. |
| 146 | 146 | rval = null; |
| 147 | 147 | } catch(IOException e) { |
| 148 | 148 | // Mostly ignore HTTP 504 error, it's just a temporary 'gateway down' error. |
| 149 | 149 | if(e.getMessage().indexOf("HTTP response code: 504")==-1) { |
| 150 | JConfig.log().handleException("Error loading data URL (" + dataURL.toString() + ')', e); | |
| 150 | JConfig.log().handleException("Error loading data URL (" + url.toString() + ')', e); | |
| 151 | 151 | } else { |
| 152 | JConfig.log().logMessage("HTTP 504 error loading URL (" + dataURL.toString() + ')'); | |
| 152 | JConfig.log().logMessage("HTTP 504 error loading URL (" + url.toString() + ')'); | |
| 153 | 153 | } |
| 154 | 154 | rval = null; |
| 155 | 155 | } |
| --- | --- | |
| 227 | 227 | /** |
| 228 | 228 | * Simplest request, load a URL, no cookie, no referer, follow redirects blindly. |
| 229 | 229 | * |
| 230 | * @param urlToGet - The URL to load. | |
| 230 | * @param url - The URL to load. | |
| 231 | 231 | * @return - A URLConnection usable to retrieve the page requested. |
| 232 | 232 | */ |
| 233 | public URLConnection getPage(String urlToGet) { | |
| 234 | return(getPage(urlToGet, null, null, true)); | |
| 233 | public URLConnection getPage(String url) { | |
| 234 | return(getPage(url, null, null, true)); | |
| 235 | 235 | } |
| 236 | 236 | |
| 237 | public URLConnection getPage(String urlToGet, String cookie, String referer, boolean redirect) { | |
| 237 | public URLConnection getPage(String url, String cookie, String referer, boolean followRedirects) { | |
| 238 | 238 | HttpURLConnection huc; |
| 239 | 239 | |
| 240 | 240 | try { |
| 241 | 241 | if(JConfig.queryConfiguration("debug.urls", "false").equals("true")) { |
| 242 | JConfig.log().logDebug("getPage: " + urlToGet); | |
| 242 | JConfig.log().logDebug("getPage: " + url); | |
| 243 | 243 | } |
| 244 | URL authURL = new URL(urlToGet); | |
| 244 | URL authURL = new URL(url); | |
| 245 | 245 | URLConnection uc = authURL.openConnection(); |
| 246 | 246 | if(!(uc instanceof HttpURLConnection)) { |
| 247 | 247 | return uc; |
| 248 | 248 | } |
| 249 | 249 | huc = (HttpURLConnection)uc; |
| 250 | huc.setInstanceFollowRedirects(redirect); | |
| 250 | huc.setInstanceFollowRedirects(followRedirects); | |
| 251 | 251 | setConnectionInfo(huc); |
| 252 | 252 | |
| 253 | 253 | huc.setRequestProperty("User-Agent", Constants.FAKE_BROWSER); |
