Permalink
Browse files

[rrliu&stlei] #12 Refactor: assign data by split reqURL

  • Loading branch information...
1 parent 57575cc commit e6aeba07d9ba8669dd9e1902a7ec82120cb9022e Li Gao committed Mar 30, 2012
Showing with 36 additions and 22 deletions.
  1. +20 −22 src/main/com/phonegap/util/HttpRequest.java
  2. +16 −0 src/test/com/phonegap/util/HttpRequestTest.java
View
42 src/main/com/phonegap/util/HttpRequest.java
@@ -1,13 +1,12 @@
package com.phonegap.util;
-import javax.microedition.io.HttpConnection;
-
public class HttpRequest {
private String method;
private String url;
private String data;
private String cookie;
+ private static final int REQUEST_FIELD_NUM = 4;
public HttpRequest() {
}
@@ -20,27 +19,12 @@ public HttpRequest(String cookie, String method, String url, String data) {
}
public void parseFrom(String reqURL) {
- reqURL = takeOffCookie(reqURL);
- reqURL = takeOffMethod(reqURL);
- this.data = takeOffUrl(reqURL).equals("null") ? null : takeOffUrl(reqURL);
- }
-
- private String takeOffUrl(String reqURL) {
- int pipeIndex = reqURL.indexOf("|");
- this.url = reqURL.substring(0, pipeIndex);
- return reqURL.substring(pipeIndex + 1);
- }
-
- private String takeOffMethod(String reqURL) {
- int pipeIndex = reqURL.indexOf("|");
- this.method = reqURL.substring(0, pipeIndex);
- return reqURL.substring(pipeIndex + 1);
- }
+ String [] requests = this.splitRequestUrl(reqURL);
- private String takeOffCookie(String reqURL) {
- int pipeIndex = reqURL.indexOf("|");
- this.cookie = reqURL.substring(0, pipeIndex);
- return reqURL.substring(pipeIndex + 1);
+ this.cookie = requests[0];
+ this.method = requests[1];
+ this.url = requests[2];
+ this.data = (requests[3].equals("null")) ? null : requests[3];
}
public void addNetworkSuffix() {
@@ -63,6 +47,20 @@ public String getData() {
return this.data;
}
+ protected String[] splitRequestUrl(String reqURL){
+ String [] result = new String [REQUEST_FIELD_NUM] ;
+ int i = 0 , pipeIndex;
+
+ while ((pipeIndex = reqURL.indexOf("|")) != -1){
+ result [i] = reqURL.substring(0, pipeIndex);
+ reqURL = reqURL.substring(pipeIndex + 1);
+ i ++;
+ }
+ result[i++] = reqURL;
+
+ return result;
+ }
+
public String getUrlWithSuffix() {
return this.url + new NetworkSuffixGenerator().generateNetworkSuffix();
}
View
16 src/test/com/phonegap/util/HttpRequestTest.java
@@ -11,6 +11,20 @@
public class HttpRequestTest extends TestCase {
+ public void test_should_split_request_correct(){
+ String reqURL = "sessionName=sessionID|POST|http://devtrac.org|postdata";
+ String[] splitReqURL = new String[] { "sessionName=sessionID", "POST",
+ "http://devtrac.org", "postdata" };
+
+ HttpRequest httpRrequest = new HttpRequest();
+ String[] splitResult = httpRrequest.splitRequestUrl(reqURL);
+ assertEquals(splitReqURL.length, splitResult.length);
+
+ for (int i = 0; i < splitReqURL.length; i++) {
+ assertEquals(splitReqURL[i], splitResult[i]);
+ }
+ }
+
public void test_should_parse_correct() {
HttpRequest httpRequest = new HttpRequest();
httpRequest.parseFrom("sessionName=sessionID|POST|http://devtrac.org|postdata");
@@ -23,10 +37,12 @@ public void test_should_parse_correct() {
public void test_should_parse_correct_when_req_without_post_data() {
HttpRequest httpRequest = new HttpRequest();
+
httpRequest.parseFrom("sessionName=sessionID|GET|http://devtrac.org|null");
assertEquals("sessionName=sessionID", httpRequest.getCookie());
assertEquals(HttpConnection.GET, httpRequest.getMethod());
assertEquals("http://devtrac.org", httpRequest.getUrl());
assertNull(httpRequest.getData());
}
+
}

0 comments on commit e6aeba0

Please sign in to comment.