@Test public void parseNetworkResponse_withStringBody_shouldLogScribeEvent() throws Exception { defaultHeaders.put(ResponseHeader.AD_TYPE.getKey(), AdType.HTML); NetworkResponse testResponse = new NetworkResponse(200, "abc".getBytes(Charset.defaultCharset()), defaultHeaders, false); subject.parseNetworkResponse(testResponse); verify(mockEventDispatcher).dispatch(any(BaseEvent.class)); }
@Override protected Response<AdResponse> parseNetworkResponse(final NetworkResponse networkResponse) { Map<String, String> headers = networkResponse.headers; if (extractBooleanHeader(headers, ResponseHeader.WARMUP, false)) { return Response.error(new MoPubNetworkError("Ad Unit is warming up.", MoPubNetworkError.Reason.WARMING_UP)); } Location location = LocationService.getLastKnownLocation(mContext, MoPub.getLocationPrecision(), MoPub.getLocationAwareness()); AdResponse.Builder builder = new AdResponse.Builder(); builder.setAdUnitId(mAdUnitId); String adTypeString = extractHeader(headers, ResponseHeader.AD_TYPE); String fullAdTypeString = extractHeader(headers, ResponseHeader.FULL_AD_TYPE); builder.setAdType(adTypeString); builder.setFullAdType(fullAdTypeString); final Integer refreshTimeSeconds = extractIntegerHeader(headers, ResponseHeader.REFRESH_TIME); final Integer refreshTimeMilliseconds = refreshTimeSeconds == null ? null : refreshTimeSeconds * 1000; builder.setRefreshTimeMilliseconds(refreshTimeMilliseconds); if (AdType.CLEAR.equals(adTypeString)) { final AdResponse adResponse = builder.build(); logScribeEvent(adResponse, networkResponse, location); return Response.error( new MoPubNetworkError( "No ads found for ad unit.", MoPubNetworkError.Reason.NO_FILL, refreshTimeMilliseconds ) ); } builder.setNetworkType(extractHeader(headers, ResponseHeader.NETWORK_TYPE)); String redirectUrl = extractHeader(headers, ResponseHeader.REDIRECT_URL); builder.setRedirectUrl(redirectUrl); String clickTrackingUrl = extractHeader(headers, ResponseHeader.CLICK_TRACKING_URL); builder.setClickTrackingUrl(clickTrackingUrl); builder.setImpressionTrackingUrl(extractHeader(headers, ResponseHeader.IMPRESSION_URL)); String failUrl = extractHeader(headers, ResponseHeader.FAIL_URL); builder.setFailoverUrl(failUrl); String requestId = getRequestId(failUrl); builder.setRequestId(requestId); boolean isScrollable = extractBooleanHeader(headers, ResponseHeader.SCROLLABLE, false); builder.setScrollable(isScrollable); builder.setDimensions(extractIntegerHeader(headers, ResponseHeader.WIDTH), extractIntegerHeader(headers, ResponseHeader.HEIGHT)); Integer adTimeoutDelaySeconds = extractIntegerHeader(headers, ResponseHeader.AD_TIMEOUT); builder.setAdTimeoutDelayMilliseconds( adTimeoutDelaySeconds == null ? null : adTimeoutDelaySeconds * 1000); String responseBody = parseStringBody(networkResponse); builder.setResponseBody(responseBody); if (AdType.NATIVE.equals(adTypeString)) { try { builder.setJsonBody(new JSONObject(responseBody)); } catch (JSONException e) { return Response.error( new MoPubNetworkError("Failed to decode body JSON for native ad format", e, MoPubNetworkError.Reason.BAD_BODY)); } } String customEventClassName = AdTypeTranslator.getCustomEventName(mAdFormat, adTypeString, fullAdTypeString, headers); builder.setCustomEventClassName(customEventClassName); String customEventData = extractHeader(headers, ResponseHeader.CUSTOM_EVENT_DATA); if (TextUtils.isEmpty(customEventData)) { customEventData = extractHeader(headers, ResponseHeader.NATIVE_PARAMS); } try { builder.setServerExtras(Json.jsonStringToMap(customEventData)); } catch (JSONException e) { return Response.error( new MoPubNetworkError("Failed to decode server extras for custom event data.", e, MoPubNetworkError.Reason.BAD_HEADER_DATA)); } if (eventDataIsInResponseBody(adTypeString, fullAdTypeString)) { Map<String, String> eventDataMap = new TreeMap<String, String>(); eventDataMap.put(DataKeys.HTML_RESPONSE_BODY_KEY, responseBody); eventDataMap.put(DataKeys.SCROLLABLE_KEY, Boolean.toString(isScrollable)); eventDataMap.put(DataKeys.CREATIVE_ORIENTATION_KEY, extractHeader(headers, ResponseHeader.ORIENTATION)); if (redirectUrl != null) { eventDataMap.put(DataKeys.REDIRECT_URL_KEY, redirectUrl); } if (clickTrackingUrl != null) { eventDataMap.put(DataKeys.CLICKTHROUGH_URL_KEY, clickTrackingUrl); } builder.setServerExtras(eventDataMap); } AdResponse adResponse = builder.build(); logScribeEvent(adResponse, networkResponse, location); return Response.success(builder.build(), HttpHeaderParser.parseCacheHeaders(networkResponse)); }
AdRequest extends Request<AdResponse> { @Override protected Response<AdResponse> parseNetworkResponse(final NetworkResponse networkResponse) { Map<String, String> headers = networkResponse.headers; if (extractBooleanHeader(headers, ResponseHeader.WARMUP, false)) { return Response.error(new MoPubNetworkError("Ad Unit is warming up.", MoPubNetworkError.Reason.WARMING_UP)); } Location location = LocationService.getLastKnownLocation(mContext, MoPub.getLocationPrecision(), MoPub.getLocationAwareness()); AdResponse.Builder builder = new AdResponse.Builder(); builder.setAdUnitId(mAdUnitId); String adTypeString = extractHeader(headers, ResponseHeader.AD_TYPE); String fullAdTypeString = extractHeader(headers, ResponseHeader.FULL_AD_TYPE); builder.setAdType(adTypeString); builder.setFullAdType(fullAdTypeString); final Integer refreshTimeSeconds = extractIntegerHeader(headers, ResponseHeader.REFRESH_TIME); final Integer refreshTimeMilliseconds = refreshTimeSeconds == null ? null : refreshTimeSeconds * 1000; builder.setRefreshTimeMilliseconds(refreshTimeMilliseconds); if (AdType.CLEAR.equals(adTypeString)) { final AdResponse adResponse = builder.build(); logScribeEvent(adResponse, networkResponse, location); return Response.error( new MoPubNetworkError( "No ads found for ad unit.", MoPubNetworkError.Reason.NO_FILL, refreshTimeMilliseconds ) ); } builder.setNetworkType(extractHeader(headers, ResponseHeader.NETWORK_TYPE)); String redirectUrl = extractHeader(headers, ResponseHeader.REDIRECT_URL); builder.setRedirectUrl(redirectUrl); String clickTrackingUrl = extractHeader(headers, ResponseHeader.CLICK_TRACKING_URL); builder.setClickTrackingUrl(clickTrackingUrl); builder.setImpressionTrackingUrl(extractHeader(headers, ResponseHeader.IMPRESSION_URL)); String failUrl = extractHeader(headers, ResponseHeader.FAIL_URL); builder.setFailoverUrl(failUrl); String requestId = getRequestId(failUrl); builder.setRequestId(requestId); boolean isScrollable = extractBooleanHeader(headers, ResponseHeader.SCROLLABLE, false); builder.setScrollable(isScrollable); builder.setDimensions(extractIntegerHeader(headers, ResponseHeader.WIDTH), extractIntegerHeader(headers, ResponseHeader.HEIGHT)); Integer adTimeoutDelaySeconds = extractIntegerHeader(headers, ResponseHeader.AD_TIMEOUT); builder.setAdTimeoutDelayMilliseconds( adTimeoutDelaySeconds == null ? null : adTimeoutDelaySeconds * 1000); String responseBody = parseStringBody(networkResponse); builder.setResponseBody(responseBody); if (AdType.NATIVE.equals(adTypeString)) { try { builder.setJsonBody(new JSONObject(responseBody)); } catch (JSONException e) { return Response.error( new MoPubNetworkError("Failed to decode body JSON for native ad format", e, MoPubNetworkError.Reason.BAD_BODY)); } } String customEventClassName = AdTypeTranslator.getCustomEventName(mAdFormat, adTypeString, fullAdTypeString, headers); builder.setCustomEventClassName(customEventClassName); String customEventData = extractHeader(headers, ResponseHeader.CUSTOM_EVENT_DATA); if (TextUtils.isEmpty(customEventData)) { customEventData = extractHeader(headers, ResponseHeader.NATIVE_PARAMS); } try { builder.setServerExtras(Json.jsonStringToMap(customEventData)); } catch (JSONException e) { return Response.error( new MoPubNetworkError("Failed to decode server extras for custom event data.", e, MoPubNetworkError.Reason.BAD_HEADER_DATA)); } if (eventDataIsInResponseBody(adTypeString, fullAdTypeString)) { Map<String, String> eventDataMap = new TreeMap<String, String>(); eventDataMap.put(DataKeys.HTML_RESPONSE_BODY_KEY, responseBody); eventDataMap.put(DataKeys.SCROLLABLE_KEY, Boolean.toString(isScrollable)); eventDataMap.put(DataKeys.CREATIVE_ORIENTATION_KEY, extractHeader(headers, ResponseHeader.ORIENTATION)); if (redirectUrl != null) { eventDataMap.put(DataKeys.REDIRECT_URL_KEY, redirectUrl); } if (clickTrackingUrl != null) { eventDataMap.put(DataKeys.CLICKTHROUGH_URL_KEY, clickTrackingUrl); } builder.setServerExtras(eventDataMap); } AdResponse adResponse = builder.build(); logScribeEvent(adResponse, networkResponse, location); return Response.success(builder.build(), HttpHeaderParser.parseCacheHeaders(networkResponse)); } }
AdRequest extends Request<AdResponse> { @Override protected Response<AdResponse> parseNetworkResponse(final NetworkResponse networkResponse) { Map<String, String> headers = networkResponse.headers; if (extractBooleanHeader(headers, ResponseHeader.WARMUP, false)) { return Response.error(new MoPubNetworkError("Ad Unit is warming up.", MoPubNetworkError.Reason.WARMING_UP)); } Location location = LocationService.getLastKnownLocation(mContext, MoPub.getLocationPrecision(), MoPub.getLocationAwareness()); AdResponse.Builder builder = new AdResponse.Builder(); builder.setAdUnitId(mAdUnitId); String adTypeString = extractHeader(headers, ResponseHeader.AD_TYPE); String fullAdTypeString = extractHeader(headers, ResponseHeader.FULL_AD_TYPE); builder.setAdType(adTypeString); builder.setFullAdType(fullAdTypeString); final Integer refreshTimeSeconds = extractIntegerHeader(headers, ResponseHeader.REFRESH_TIME); final Integer refreshTimeMilliseconds = refreshTimeSeconds == null ? null : refreshTimeSeconds * 1000; builder.setRefreshTimeMilliseconds(refreshTimeMilliseconds); if (AdType.CLEAR.equals(adTypeString)) { final AdResponse adResponse = builder.build(); logScribeEvent(adResponse, networkResponse, location); return Response.error( new MoPubNetworkError( "No ads found for ad unit.", MoPubNetworkError.Reason.NO_FILL, refreshTimeMilliseconds ) ); } builder.setNetworkType(extractHeader(headers, ResponseHeader.NETWORK_TYPE)); String redirectUrl = extractHeader(headers, ResponseHeader.REDIRECT_URL); builder.setRedirectUrl(redirectUrl); String clickTrackingUrl = extractHeader(headers, ResponseHeader.CLICK_TRACKING_URL); builder.setClickTrackingUrl(clickTrackingUrl); builder.setImpressionTrackingUrl(extractHeader(headers, ResponseHeader.IMPRESSION_URL)); String failUrl = extractHeader(headers, ResponseHeader.FAIL_URL); builder.setFailoverUrl(failUrl); String requestId = getRequestId(failUrl); builder.setRequestId(requestId); boolean isScrollable = extractBooleanHeader(headers, ResponseHeader.SCROLLABLE, false); builder.setScrollable(isScrollable); builder.setDimensions(extractIntegerHeader(headers, ResponseHeader.WIDTH), extractIntegerHeader(headers, ResponseHeader.HEIGHT)); Integer adTimeoutDelaySeconds = extractIntegerHeader(headers, ResponseHeader.AD_TIMEOUT); builder.setAdTimeoutDelayMilliseconds( adTimeoutDelaySeconds == null ? null : adTimeoutDelaySeconds * 1000); String responseBody = parseStringBody(networkResponse); builder.setResponseBody(responseBody); if (AdType.NATIVE.equals(adTypeString)) { try { builder.setJsonBody(new JSONObject(responseBody)); } catch (JSONException e) { return Response.error( new MoPubNetworkError("Failed to decode body JSON for native ad format", e, MoPubNetworkError.Reason.BAD_BODY)); } } String customEventClassName = AdTypeTranslator.getCustomEventName(mAdFormat, adTypeString, fullAdTypeString, headers); builder.setCustomEventClassName(customEventClassName); String customEventData = extractHeader(headers, ResponseHeader.CUSTOM_EVENT_DATA); if (TextUtils.isEmpty(customEventData)) { customEventData = extractHeader(headers, ResponseHeader.NATIVE_PARAMS); } try { builder.setServerExtras(Json.jsonStringToMap(customEventData)); } catch (JSONException e) { return Response.error( new MoPubNetworkError("Failed to decode server extras for custom event data.", e, MoPubNetworkError.Reason.BAD_HEADER_DATA)); } if (eventDataIsInResponseBody(adTypeString, fullAdTypeString)) { Map<String, String> eventDataMap = new TreeMap<String, String>(); eventDataMap.put(DataKeys.HTML_RESPONSE_BODY_KEY, responseBody); eventDataMap.put(DataKeys.SCROLLABLE_KEY, Boolean.toString(isScrollable)); eventDataMap.put(DataKeys.CREATIVE_ORIENTATION_KEY, extractHeader(headers, ResponseHeader.ORIENTATION)); if (redirectUrl != null) { eventDataMap.put(DataKeys.REDIRECT_URL_KEY, redirectUrl); } if (clickTrackingUrl != null) { eventDataMap.put(DataKeys.CLICKTHROUGH_URL_KEY, clickTrackingUrl); } builder.setServerExtras(eventDataMap); } AdResponse adResponse = builder.build(); logScribeEvent(adResponse, networkResponse, location); return Response.success(builder.build(), HttpHeaderParser.parseCacheHeaders(networkResponse)); } AdRequest(@NonNull final String url,
            @NonNull final AdFormat adFormat,
            @Nullable final String adUnitId,
            @NonNull Context context,
            @NonNull final Listener listener); }
AdRequest extends Request<AdResponse> { @Override protected Response<AdResponse> parseNetworkResponse(final NetworkResponse networkResponse) { Map<String, String> headers = networkResponse.headers; if (extractBooleanHeader(headers, ResponseHeader.WARMUP, false)) { return Response.error(new MoPubNetworkError("Ad Unit is warming up.", MoPubNetworkError.Reason.WARMING_UP)); } Location location = LocationService.getLastKnownLocation(mContext, MoPub.getLocationPrecision(), MoPub.getLocationAwareness()); AdResponse.Builder builder = new AdResponse.Builder(); builder.setAdUnitId(mAdUnitId); String adTypeString = extractHeader(headers, ResponseHeader.AD_TYPE); String fullAdTypeString = extractHeader(headers, ResponseHeader.FULL_AD_TYPE); builder.setAdType(adTypeString); builder.setFullAdType(fullAdTypeString); final Integer refreshTimeSeconds = extractIntegerHeader(headers, ResponseHeader.REFRESH_TIME); final Integer refreshTimeMilliseconds = refreshTimeSeconds == null ? null : refreshTimeSeconds * 1000; builder.setRefreshTimeMilliseconds(refreshTimeMilliseconds); if (AdType.CLEAR.equals(adTypeString)) { final AdResponse adResponse = builder.build(); logScribeEvent(adResponse, networkResponse, location); return Response.error( new MoPubNetworkError( "No ads found for ad unit.", MoPubNetworkError.Reason.NO_FILL, refreshTimeMilliseconds ) ); } builder.setNetworkType(extractHeader(headers, ResponseHeader.NETWORK_TYPE)); String redirectUrl = extractHeader(headers, ResponseHeader.REDIRECT_URL); builder.setRedirectUrl(redirectUrl); String clickTrackingUrl = extractHeader(headers, ResponseHeader.CLICK_TRACKING_URL); builder.setClickTrackingUrl(clickTrackingUrl); builder.setImpressionTrackingUrl(extractHeader(headers, ResponseHeader.IMPRESSION_URL)); String failUrl = extractHeader(headers, ResponseHeader.FAIL_URL); builder.setFailoverUrl(failUrl); String requestId = getRequestId(failUrl); builder.setRequestId(requestId); boolean isScrollable = extractBooleanHeader(headers, ResponseHeader.SCROLLABLE, false); builder.setScrollable(isScrollable); builder.setDimensions(extractIntegerHeader(headers, ResponseHeader.WIDTH), extractIntegerHeader(headers, ResponseHeader.HEIGHT)); Integer adTimeoutDelaySeconds = extractIntegerHeader(headers, ResponseHeader.AD_TIMEOUT); builder.setAdTimeoutDelayMilliseconds( adTimeoutDelaySeconds == null ? null : adTimeoutDelaySeconds * 1000); String responseBody = parseStringBody(networkResponse); builder.setResponseBody(responseBody); if (AdType.NATIVE.equals(adTypeString)) { try { builder.setJsonBody(new JSONObject(responseBody)); } catch (JSONException e) { return Response.error( new MoPubNetworkError("Failed to decode body JSON for native ad format", e, MoPubNetworkError.Reason.BAD_BODY)); } } String customEventClassName = AdTypeTranslator.getCustomEventName(mAdFormat, adTypeString, fullAdTypeString, headers); builder.setCustomEventClassName(customEventClassName); String customEventData = extractHeader(headers, ResponseHeader.CUSTOM_EVENT_DATA); if (TextUtils.isEmpty(customEventData)) { customEventData = extractHeader(headers, ResponseHeader.NATIVE_PARAMS); } try { builder.setServerExtras(Json.jsonStringToMap(customEventData)); } catch (JSONException e) { return Response.error( new MoPubNetworkError("Failed to decode server extras for custom event data.", e, MoPubNetworkError.Reason.BAD_HEADER_DATA)); } if (eventDataIsInResponseBody(adTypeString, fullAdTypeString)) { Map<String, String> eventDataMap = new TreeMap<String, String>(); eventDataMap.put(DataKeys.HTML_RESPONSE_BODY_KEY, responseBody); eventDataMap.put(DataKeys.SCROLLABLE_KEY, Boolean.toString(isScrollable)); eventDataMap.put(DataKeys.CREATIVE_ORIENTATION_KEY, extractHeader(headers, ResponseHeader.ORIENTATION)); if (redirectUrl != null) { eventDataMap.put(DataKeys.REDIRECT_URL_KEY, redirectUrl); } if (clickTrackingUrl != null) { eventDataMap.put(DataKeys.CLICKTHROUGH_URL_KEY, clickTrackingUrl); } builder.setServerExtras(eventDataMap); } AdResponse adResponse = builder.build(); logScribeEvent(adResponse, networkResponse, location); return Response.success(builder.build(), HttpHeaderParser.parseCacheHeaders(networkResponse)); } AdRequest(@NonNull final String url,
            @NonNull final AdFormat adFormat,
            @Nullable final String adUnitId,
            @NonNull Context context,
            @NonNull final Listener listener); @NonNull Listener getListener(); @Override Map<String, String> getHeaders(); }
AdRequest extends Request<AdResponse> { @Override protected Response<AdResponse> parseNetworkResponse(final NetworkResponse networkResponse) { Map<String, String> headers = networkResponse.headers; if (extractBooleanHeader(headers, ResponseHeader.WARMUP, false)) { return Response.error(new MoPubNetworkError("Ad Unit is warming up.", MoPubNetworkError.Reason.WARMING_UP)); } Location location = LocationService.getLastKnownLocation(mContext, MoPub.getLocationPrecision(), MoPub.getLocationAwareness()); AdResponse.Builder builder = new AdResponse.Builder(); builder.setAdUnitId(mAdUnitId); String adTypeString = extractHeader(headers, ResponseHeader.AD_TYPE); String fullAdTypeString = extractHeader(headers, ResponseHeader.FULL_AD_TYPE); builder.setAdType(adTypeString); builder.setFullAdType(fullAdTypeString); final Integer refreshTimeSeconds = extractIntegerHeader(headers, ResponseHeader.REFRESH_TIME); final Integer refreshTimeMilliseconds = refreshTimeSeconds == null ? null : refreshTimeSeconds * 1000; builder.setRefreshTimeMilliseconds(refreshTimeMilliseconds); if (AdType.CLEAR.equals(adTypeString)) { final AdResponse adResponse = builder.build(); logScribeEvent(adResponse, networkResponse, location); return Response.error( new MoPubNetworkError( "No ads found for ad unit.", MoPubNetworkError.Reason.NO_FILL, refreshTimeMilliseconds ) ); } builder.setNetworkType(extractHeader(headers, ResponseHeader.NETWORK_TYPE)); String redirectUrl = extractHeader(headers, ResponseHeader.REDIRECT_URL); builder.setRedirectUrl(redirectUrl); String clickTrackingUrl = extractHeader(headers, ResponseHeader.CLICK_TRACKING_URL); builder.setClickTrackingUrl(clickTrackingUrl); builder.setImpressionTrackingUrl(extractHeader(headers, ResponseHeader.IMPRESSION_URL)); String failUrl = extractHeader(headers, ResponseHeader.FAIL_URL); builder.setFailoverUrl(failUrl); String requestId = getRequestId(failUrl); builder.setRequestId(requestId); boolean isScrollable = extractBooleanHeader(headers, ResponseHeader.SCROLLABLE, false); builder.setScrollable(isScrollable); builder.setDimensions(extractIntegerHeader(headers, ResponseHeader.WIDTH), extractIntegerHeader(headers, ResponseHeader.HEIGHT)); Integer adTimeoutDelaySeconds = extractIntegerHeader(headers, ResponseHeader.AD_TIMEOUT); builder.setAdTimeoutDelayMilliseconds( adTimeoutDelaySeconds == null ? null : adTimeoutDelaySeconds * 1000); String responseBody = parseStringBody(networkResponse); builder.setResponseBody(responseBody); if (AdType.NATIVE.equals(adTypeString)) { try { builder.setJsonBody(new JSONObject(responseBody)); } catch (JSONException e) { return Response.error( new MoPubNetworkError("Failed to decode body JSON for native ad format", e, MoPubNetworkError.Reason.BAD_BODY)); } } String customEventClassName = AdTypeTranslator.getCustomEventName(mAdFormat, adTypeString, fullAdTypeString, headers); builder.setCustomEventClassName(customEventClassName); String customEventData = extractHeader(headers, ResponseHeader.CUSTOM_EVENT_DATA); if (TextUtils.isEmpty(customEventData)) { customEventData = extractHeader(headers, ResponseHeader.NATIVE_PARAMS); } try { builder.setServerExtras(Json.jsonStringToMap(customEventData)); } catch (JSONException e) { return Response.error( new MoPubNetworkError("Failed to decode server extras for custom event data.", e, MoPubNetworkError.Reason.BAD_HEADER_DATA)); } if (eventDataIsInResponseBody(adTypeString, fullAdTypeString)) { Map<String, String> eventDataMap = new TreeMap<String, String>(); eventDataMap.put(DataKeys.HTML_RESPONSE_BODY_KEY, responseBody); eventDataMap.put(DataKeys.SCROLLABLE_KEY, Boolean.toString(isScrollable)); eventDataMap.put(DataKeys.CREATIVE_ORIENTATION_KEY, extractHeader(headers, ResponseHeader.ORIENTATION)); if (redirectUrl != null) { eventDataMap.put(DataKeys.REDIRECT_URL_KEY, redirectUrl); } if (clickTrackingUrl != null) { eventDataMap.put(DataKeys.CLICKTHROUGH_URL_KEY, clickTrackingUrl); } builder.setServerExtras(eventDataMap); } AdResponse adResponse = builder.build(); logScribeEvent(adResponse, networkResponse, location); return Response.success(builder.build(), HttpHeaderParser.parseCacheHeaders(networkResponse)); } AdRequest(@NonNull final String url,
            @NonNull final AdFormat adFormat,
            @Nullable final String adUnitId,
            @NonNull Context context,
            @NonNull final Listener listener); @NonNull Listener getListener(); @Override Map<String, String> getHeaders();  }