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.

by mrs, 08 Nov, 2009 09:24 AM
1027 1032  
1515 import com.jbidwatcher.util.xml.XMLInterface;
1616 import com.jbidwatcher.util.http.Http;
1717 import com.jbidwatcher.util.http.ClientHttpRequest;
18 import com.jbidwatcher.util.http.HttpInterface;
1819 import com.jbidwatcher.auction.AuctionEntry;
1920 import com.jbidwatcher.auction.EntryCorral;
2021 
------
3536  */
3637 public class MyJBidwatcher {
3738   private static MyJBidwatcher sInstance = null;
38   private Http mNet = null;
39   private HttpInterface mNet = null;
3940   private static String LOG_UPLOAD_URL =  "my.jbidwatcher.com/upload/log";
4041   private static String ITEM_UPLOAD_URL = "my.jbidwatcher.com/upload/listing";
4142   private static String SYNC_UPLOAD_URL = "my.jbidwatcher.com/upload/sync";
------
5354     return "http://" + url;
5455   }
5556 
56   private Http http() {
57   private HttpInterface http() {
5758     if(mNet == null) {
5859       mNet = new Http();
5960     }
996 1032  
1212 import java.io.IOException;
1313 
1414 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;
1818 
1919   public CookieJar() {
20     _cookies = new TreeMap<String, Cookie>();
20     mCookies = new TreeMap<String, Cookie>();
2121   }
2222 
2323   public Cookie getCookie(String keyName) {
24     return _cookies.get(keyName);
24     return mCookies.get(keyName);
2525   }
2626 
2727   public String dump() {
2828     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);
3131       rval.append(cookieName).append(": ").append(value.getValue()).append('\n');
3232     }
3333     return rval.toString();
------
163163     do {
164164       nextKey = uc.getHeaderFieldKey(i);
165165       if(nextKey != null) {
166         if(do_uber_debug) {
166         if(sUberDebug) {
167167           JConfig.log().logDebug(nextKey+": " + uc.getHeaderField(i));
168168         }
169169         //  If we're redirected, shortcut to loading the new page.
------
179179         if(nextKey.startsWith("Set-Cookie") ||
180180            nextKey.startsWith("Set-cookie")) {
181181           Cookie newCookie = new Cookie(uc.getHeaderField(i));
182           _cookies.put(newCookie.getKey(), newCookie);
182           mCookies.put(newCookie.getKey(), newCookie);
183183         }
184184       }
185185       i++;
------
215215   }
216216 
217217   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();
219220 
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);
226223     } 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);
232225     }
233     return uc;
226 
227     return (HttpURLConnection)uc;
234228   }
235229 
236230   public String toString() {
237231     boolean firstThrough = true;
238232     StringBuffer outBuf = null;
239233 
240     for (Cookie cookie : _cookies.values()) {
234     for (Cookie cookie : mCookies.values()) {
241235       if (cookie.getValue().length() != 0) {
242236         if (!firstThrough) {
243237           outBuf.append("; ");
964 1032  
1616 import java.io.*;
1717 import java.util.Map;
1818 
19 public class Http {
19 public class Http implements HttpInterface {
2020   private String mUsername = null;
2121   private String mPassword = null;
2222 
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; }
2525 
2626   public void setAuthInfo(String user, String pass) {
2727     mUsername = user;
------
5454     }
5555   }
5656 
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) {
5858     URLConnection huc;
5959 
6060     try {
6161       if(JConfig.queryConfiguration("debug.urls", "false").equals("true")) {
62         JConfig.log().logDebug("postFormPage: " + urlToPost);
62         JConfig.log().logDebug("postFormPage: " + url);
6363       }
64       URL authURL = new URL(urlToPost);
64       URL authURL = new URL(url);
6565 
6666       huc = authURL.openConnection();
6767       setConnectionInfo(huc);
------
7070       if(huc instanceof HttpURLConnection) {
7171         HttpURLConnection conn = (HttpURLConnection)huc;
7272         conn.setRequestMethod("POST");
73         if(!follow_redirects) conn.setInstanceFollowRedirects(false);
73         if(!followRedirects) conn.setInstanceFollowRedirects(false);
7474       }
7575       if(JConfig.queryConfiguration("debug.uber", "false").equals("true") && JConfig.debugging) {
7676         dumpFormHeaders(System.err, cgiData, cookie);
------
124124     return uc;
125125   }
126126 
127   public ByteBuffer getURL(URL dataURL) {
128     return getURL(dataURL, null);
127   public ByteBuffer getURL(URL url) {
128     return getURL(url, null);
129129   }
130130 
131131   /** 
132132    * @brief Retrieve data from HTTP in raw byte form.
133133    * 
134    * @param dataURL - The URL of the raw data to retrieve.
134    * @param url - The URL of the raw data to retrieve.
135135    * @param inCookie - Any cookie needed to be passed along.
136136    * 
137137    * @return - A result with raw data and the length.
138138    */
139   private ByteBuffer getURL(URL dataURL, String inCookie) {
139   private ByteBuffer getURL(URL url, String inCookie) {
140140     ByteBuffer rval;
141141 
142142     try {
143       rval = receiveData(makeRequest(dataURL, inCookie));
143       rval = receiveData(makeRequest(url, inCookie));
144144     } catch(FileNotFoundException fnfe) {
145145       //  It'd be great if we could pass along something that said, 'not here, never will be'.
146146       rval = null;
147147     } catch(IOException e) {
148148       //  Mostly ignore HTTP 504 error, it's just a temporary 'gateway down' error.
149149       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);
151151       } else {
152         JConfig.log().logMessage("HTTP 504 error loading URL (" + dataURL.toString() + ')');
152         JConfig.log().logMessage("HTTP 504 error loading URL (" + url.toString() + ')');
153153       }
154154       rval = null;
155155     }
------
227227   /**
228228    * Simplest request, load a URL, no cookie, no referer, follow redirects blindly.
229229    *
230    * @param urlToGet - The URL to load.
230    * @param url - The URL to load.
231231    * @return - A URLConnection usable to retrieve the page requested.
232232    */
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));
235235   }
236236 
237   public URLConnection getPage(String urlToGet, String cookie, String referer, boolean redirect) {
237   public URLConnection getPage(String url, String cookie, String referer, boolean followRedirects) {
238238     HttpURLConnection huc;
239239 
240240     try {
241241       if(JConfig.queryConfiguration("debug.urls", "false").equals("true")) {
242         JConfig.log().logDebug("getPage: " + urlToGet);
242         JConfig.log().logDebug("getPage: " + url);
243243       }
244       URL authURL = new URL(urlToGet);
244       URL authURL = new URL(url);
245245       URLConnection uc = authURL.openConnection();
246246       if(!(uc instanceof HttpURLConnection)) {
247247         return uc;
248248       }
249249       huc = (HttpURLConnection)uc;
250       huc.setInstanceFollowRedirects(redirect);
250       huc.setInstanceFollowRedirects(followRedirects);
251251       setConnectionInfo(huc);
252252 
253253       huc.setRequestProperty("User-Agent", Constants.FAKE_BROWSER);