WXStreamModule extends WXModule { @WXModuleAnno public void fetch(String optionsStr, final JSCallback callback, JSCallback progressCallback){ JSONObject optionsObj = null; try { optionsObj = JSON.parseObject(optionsStr); }catch (JSONException e){ e.printStackTrace(); } boolean invaildOption = optionsObj==null || optionsObj.getString("url")==null; if(invaildOption){ if(callback != null) { Map<String, Object> resp = new HashMap<>(); resp.put("ok", false); resp.put(STATUS_TEXT, Status.ERR_INVALID_REQUEST); callback.invoke(resp); } return; } String method = optionsObj.getString("method"); String url = optionsObj.getString("url"); JSONObject headers = optionsObj.getJSONObject("headers"); String body = optionsObj.getString("body"); String type = optionsObj.getString("type"); Options.Builder builder = new Options.Builder() .setMethod(!"GET".equals(method)&&!"POST".equals(method)?"GET":method) .setUrl(url) .setBody(body) .setType(type); extractHeaders(headers,builder); sendRequest(builder.createOptions(), new ResponseCallback() { @Override public void onResponse(WXResponse response, Map<String, String> headers) { if(callback != null) { Map<String, Object> resp = new HashMap<>(); if(response == null|| "-1".equals(response.statusCode)){ resp.put(STATUS,"-1"); resp.put(STATUS_TEXT,Status.ERR_CONNECT_FAILED); }else { resp.put(STATUS, response.statusCode); int code = Integer.parseInt(response.statusCode); resp.put("ok", (code >= 200 && code <= 299)); if (response.originalData == null) { resp.put("data", null); } else { resp.put("data", readAsString(response.originalData, headers!=null?headers.get("Content-Type"):"" )); } resp.put(STATUS_TEXT, Status.getStatusText(response.statusCode)); } resp.put("headers", headers); callback.invoke(resp); } } }, progressCallback); } WXStreamModule();  WXStreamModule(IWXHttpAdapter adapter); @Deprecated @WXModuleAnno void sendHttp(String params, final String callback); @WXModuleAnno void fetch(String optionsStr, final JSCallback callback, JSCallback progressCallback); static final String STATUS_TEXT; static final String STATUS; }
@Test public void testFetchInvaildOptions() throws Exception{ IWXHttpAdapter adapter = new IWXHttpAdapter() { @Override public void sendRequest(WXRequest request, OnHttpListener listener) { listener.onHttpFinish(successResponse()); } }; WXStreamModule streamModule = new WXStreamModule(adapter); Callback cb = new Callback(); streamModule.fetch("",cb,null); assert !(boolean)cb.mData.get("ok"); }
@Test public void testFetchSuccessFinish() throws Exception{ IWXHttpAdapter adapter = new IWXHttpAdapter() { @Override public void sendRequest(WXRequest request, OnHttpListener listener) { listener.onHttpFinish(successResponse()); } }; WXStreamModule streamModule = new WXStreamModule(adapter); Callback cb = new Callback(); streamModule.fetch("{'url':'http: assert (boolean)cb.mData.get("ok"); }
@Test public void testFetchHeaderReceived() throws Exception{ IWXHttpAdapter adapter = new IWXHttpAdapter() { @Override public void sendRequest(WXRequest request, OnHttpListener listener) { Map<String,List<String>> headers = new HashMap<>(); headers.put("key", Arrays.asList("someval")); listener.onHeadersReceived(200,headers); } }; WXStreamModule streamModule = new WXStreamModule(adapter); Callback cb = new Callback(); streamModule.fetch("{'url':'http: assert ((Map<String,String>)cb.mData.get("headers")).get("key").equals("someval"); }
@Test public void testFetchRequestHttpbinCallback() throws Exception{ WXStreamModule streamModule = new WXStreamModule(new DefaultWXHttpAdapter()); JSCallback progress = mock(JSCallback.class); JSCallback finish = mock(JSCallback.class); System.out.print("request start "+System.currentTimeMillis()); streamModule.fetch("{method: 'POST',url: 'http: verify(progress,timeout(10*1000).atLeastOnce()).invokeAndKeepAlive(anyMapOf(String.class, Object.class)); verify(finish,timeout(10*1000).times(1)).invoke(anyMapOf(String.class, Object.class)); System.out.print("\nrequest finish"+System.currentTimeMillis()); }
@Test public void testFetchStatus() throws Exception{ WXStreamModule streamModule = new WXStreamModule(new IWXHttpAdapter() { @Override public void sendRequest(WXRequest request, OnHttpListener listener) { WXResponse response = new WXResponse(); response.statusCode = "-1"; listener.onHttpFinish(response); } }); Callback finish = new Callback(); streamModule.fetch("",finish,null); assertEquals(finish.mData.get(WXStreamModule.STATUS_TEXT),Status.ERR_INVALID_REQUEST); streamModule.fetch("{method: 'POST',url: 'http: assertEquals(finish.mData.get(WXStreamModule.STATUS_TEXT),Status.ERR_CONNECT_FAILED); streamModule = new WXStreamModule(new IWXHttpAdapter() { @Override public void sendRequest(WXRequest request, OnHttpListener listener) { WXResponse response = new WXResponse(); response.statusCode = "302"; listener.onHttpFinish(response); } }); streamModule.fetch("{method: 'POST',url: 'http: assertEquals(finish.mData.get(WXStreamModule.STATUS_TEXT),Status.getStatusText("302")); }