### Question:
JsRequestBuilder { public JsRequest build(HttpServletRequest request) throws GadgetException { JsUri jsUri = jsUriManager.processExternJsUri(new UriBuilder(request).toUri()); String host = request.getHeader("Host"); boolean inCache = request.getHeader("If-Modified-Since") != null; return build(jsUri, host, inCache); } @Inject  JsRequestBuilder(JsUriManager jsUriManager,
                          FeatureRegistry registry); JsRequest build(HttpServletRequest request); JsRequest build(JsUri jsUri, String host);  }### Answer:
@Test public void testCreateRequestNotInCache() throws Exception { EasyMock.expect(jsUriManager.processExternJsUri(EasyMock.isA(Uri.class))).andReturn(jsUri); EasyMock.expect(request.getHeader(HOST_HEADER_KEY)).andReturn(HOST); EasyMock.expect(request.getHeader(IMS_HEADER_KEY)).andReturn(null); control.replay(); JsRequest jsRequest = builder.build(request); control.verify(); assertSame(jsUri, jsRequest.getJsUri()); assertEquals(HOST, jsRequest.getHost()); assertFalse(jsRequest.isInCache()); }
@Test public void testCreateRequestInCache() throws Exception { EasyMock.expect(jsUriManager.processExternJsUri(EasyMock.isA(Uri.class))).andReturn(jsUri); EasyMock.expect(request.getHeader(HOST_HEADER_KEY)).andReturn(HOST); EasyMock.expect(request.getHeader(IMS_HEADER_KEY)).andReturn("today"); control.replay(); JsRequest jsRequest = builder.build(request); control.verify(); assertSame(jsUri, jsRequest.getJsUri()); assertEquals(HOST, jsRequest.getHost()); assertTrue(jsRequest.isInCache()); }
@Test public void testCreateRequestThrowsExceptionOnParseError() throws Exception { EasyMock.expect(jsUriManager.processExternJsUri(EasyMock.isA(Uri.class))).andThrow( new GadgetException(Code.INVALID_PARAMETER)); control.replay(); try { builder.build(request); fail("Should have thrown a GadgetException."); } catch (GadgetException e) { } control.verify(); }
@Test public void testCreateRequestComputesDeps() throws Exception { List<String> requested = Lists.newArrayList("req1", "req2"); List<String> loaded = Lists.newArrayList("load1", "load2"); List<String> fullClosure = Lists.newArrayList("dep-s1", "dep1", "dep2", "dep-s2", "load1", "load2", "req1", "req2"); List<String> loadedClosure = Lists.newArrayList("dep-s1", "dep-s2", "load1", "load2"); EasyMock.expect(registry.getFeatures(requested)).andReturn(fullClosure); EasyMock.expect(registry.getFeatures(loaded)).andReturn(loadedClosure); EasyMock.expect(jsUri.getLibs()).andReturn(requested); EasyMock.expect(jsUri.getLoadedLibs()).andReturn(loaded); EasyMock.expect(jsUriManager.processExternJsUri(EasyMock.isA(Uri.class))).andReturn(jsUri); EasyMock.expect(request.getHeader(IMS_HEADER_KEY)).andReturn(null); EasyMock.expect(request.getHeader(HOST_HEADER_KEY)).andReturn(HOST); control.replay(); JsRequest jsRequest = builder.build(request); assertSame(jsUri, jsRequest.getJsUri()); assertEquals(HOST, jsRequest.getHost()); List<String> allMatch = Lists.newArrayList("dep-s1", "dep1", "dep2", "dep-s2", "load1", "load2", "req1", "req2"); assertEquals(allMatch, jsRequest.getAllFeatures()); List<String> loadedMatch = Lists.newArrayList("dep-s1", "dep-s2", "load1", "load2"); assertEquals(loadedMatch, jsRequest.getLoadedFeatures()); List<String> newMatch = Lists.newArrayList("dep1", "dep2", "req1", "req2"); assertEquals(newMatch, jsRequest.getNewFeatures()); control.verify(); }