Java Code Examples for org.springframework.test.web.servlet.request.MockMvcRequestBuilders#get()
The following examples show how to use
org.springframework.test.web.servlet.request.MockMvcRequestBuilders#get() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HtmlUnitRequestBuilder.java From spring-analysis-note with MIT License | 6 votes |
@Override public Object merge(@Nullable Object parent) { if (parent instanceof RequestBuilder) { if (parent instanceof MockHttpServletRequestBuilder) { MockHttpServletRequestBuilder copiedParent = MockMvcRequestBuilders.get("/"); copiedParent.merge(parent); this.parentBuilder = copiedParent; } else { this.parentBuilder = (RequestBuilder) parent; } if (parent instanceof SmartRequestBuilder) { this.parentPostProcessor = (SmartRequestBuilder) parent; } } return this; }
Example 2
Source File: HtmlUnitRequestBuilder.java From java-technology-stack with MIT License | 6 votes |
@Override public Object merge(@Nullable Object parent) { if (parent instanceof RequestBuilder) { if (parent instanceof MockHttpServletRequestBuilder) { MockHttpServletRequestBuilder copiedParent = MockMvcRequestBuilders.get("/"); copiedParent.merge(parent); this.parentBuilder = copiedParent; } else { this.parentBuilder = (RequestBuilder) parent; } if (parent instanceof SmartRequestBuilder) { this.parentPostProcessor = (SmartRequestBuilder) parent; } } return this; }
Example 3
Source File: AbstractMockMvcBuilder.java From java-technology-stack with MIT License | 6 votes |
/** * Build a {@link org.springframework.test.web.servlet.MockMvc} instance. */ @Override @SuppressWarnings("rawtypes") public final MockMvc build() { WebApplicationContext wac = initWebAppContext(); ServletContext servletContext = wac.getServletContext(); MockServletConfig mockServletConfig = new MockServletConfig(servletContext); for (MockMvcConfigurer configurer : this.configurers) { RequestPostProcessor processor = configurer.beforeMockMvcCreated(this, wac); if (processor != null) { if (this.defaultRequestBuilder == null) { this.defaultRequestBuilder = MockMvcRequestBuilders.get("/"); } if (this.defaultRequestBuilder instanceof ConfigurableSmartRequestBuilder) { ((ConfigurableSmartRequestBuilder) this.defaultRequestBuilder).with(processor); } } } Filter[] filterArray = this.filters.toArray(new Filter[0]); return super.createMockMvc(filterArray, mockServletConfig, wac, this.defaultRequestBuilder, this.globalResultMatchers, this.globalResultHandlers, this.dispatcherServletCustomizers); }
Example 4
Source File: RecentlyUsedSearchTest.java From rebuild with GNU General Public License v3.0 | 5 votes |
@Test public void testClean() throws Exception { MockHttpServletRequestBuilder builder = MockMvcRequestBuilders .get("/commons/search/recently-clean?entity=" + EntityHelper.User); MvcResponse resp = perform(builder, UserService.ADMIN_USER); System.out.println(resp); Assert.assertTrue(resp.isSuccess()); }
Example 5
Source File: RecentlyUsedSearchTest.java From rebuild with GNU General Public License v3.0 | 5 votes |
@Test public void testGet() throws Exception { MockHttpServletRequestBuilder builder = MockMvcRequestBuilders .get("/commons/search/recently?entity=User"); MvcResponse resp = perform(builder, UserService.ADMIN_USER); System.out.println(resp); Assert.assertTrue(resp.isSuccess()); }
Example 6
Source File: GeneralPageTest.java From rebuild with GNU General Public License v3.0 | 5 votes |
@Test public void testViewPage() throws Exception { ID testUser = UserService.ADMIN_USER; Entity testEntity = MetadataHelper.getEntity(TEST_ENTITY); Record testRecord = EntityHelper.forNew(testEntity.getEntityCode(), testUser); Application.getSessionStore().set(testUser); testRecord = Application.getEntityService(testEntity.getEntityCode()).create(testRecord); Application.getSessionStore().clean(); MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/app/TestAllFields/view/" + testRecord.getPrimary()); System.out.println(perform(builder, testUser)); }
Example 7
Source File: ServerInfoControllerTest.java From tds with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void serverVersionRequestTest() throws Exception { requestBuilder = MockMvcRequestBuilders.get("/info/serverVersion.txt"); MvcResult mvc = this.mockMvc.perform(requestBuilder).andReturn(); assertEquals(200, mvc.getResponse().getStatus()); checkModelAndView(mvc.getModelAndView(), "thredds/server/serverinfo/serverVersion_txt"); }
Example 8
Source File: DatasetBoundariesTest.java From tds with BSD 3-Clause "New" or "Revised" License | 5 votes |
private Object[] parameters() { return new Object[] { new Object[] {"/datasetBoundaries.xml", MockMvcRequestBuilders.get(datasetPath + "/datasetBoundaries.xml"), SupportedFormat.WKT, expectedWKT}, new Object[] {"/datasetBoundaries.xml?accept=wkt", MockMvcRequestBuilders.get(datasetPath + "/datasetBoundaries.xml").param("accept", "wkt"), SupportedFormat.WKT, expectedWKT}, new Object[] {"/datasetBoundaries.xml?accept=json", MockMvcRequestBuilders.get(datasetPath + "/datasetBoundaries.xml").param("accept", "json"), SupportedFormat.JSON, expectedGeoJSON}, new Object[] {"/datasetBoundaries.wkt", MockMvcRequestBuilders.get(datasetPath + "/datasetBoundaries.wkt"), SupportedFormat.WKT, expectedWKT}, new Object[] {"/datasetBoundaries.json", MockMvcRequestBuilders.get(datasetPath + "/datasetBoundaries.json"), SupportedFormat.JSON, expectedGeoJSON}}; }
Example 9
Source File: RootControllerTest.java From tds with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testRootRedirect() throws Exception { requestBuilder = MockMvcRequestBuilders.get("/"); MvcResult mvc = this.mockMvc.perform(requestBuilder).andReturn(); // Check that "/" is redirected assertEquals(302, mvc.getResponse().getStatus()); assertEquals("redirect:/catalog/catalog.html", mvc.getModelAndView().getViewName()); }
Example 10
Source File: RootControllerTest.java From tds with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testStaticContent() throws Exception { requestBuilder = MockMvcRequestBuilders.get("/tdsCat.css"); MvcResult mvc = this.mockMvc.perform(requestBuilder).andReturn(); // Check that "/" is redirected Assert.assertEquals(200, mvc.getResponse().getStatus()); String content = mvc.getResponse().getContentAsString(); System.out.printf("content='%s'%n", content); // Assert.assertNotNull(mvc.getModelAndView()); // assertEquals("redirect:/catalog/catalog.html", mvc.getModelAndView().getViewName()); }
Example 11
Source File: SigninControllTest.java From rebuild with GNU General Public License v3.0 | 4 votes |
@Test public void testUnLogin() throws Exception { MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/dashboard/home"); System.out.println(perform(builder, null, MockMvcResultMatchers.status().is3xxRedirection())); }
Example 12
Source File: AppUtilsTest.java From rebuild with GNU General Public License v3.0 | 4 votes |
@Test public void testGetErrorMessage() throws Exception { MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/dashboard/home"); MvcResult result = springMVC.perform(builder).andReturn(); System.out.println(AppUtils.getErrorMessage(result.getRequest(), null)); }
Example 13
Source File: GatewayFlowRuleControllerTest.java From Sentinel with Apache License 2.0 | 4 votes |
@Test public void testQueryFlowRules() throws Exception { String path = "/gateway/flow/list.json"; List<GatewayFlowRuleEntity> entities = new ArrayList<>(); // Mock two entities GatewayFlowRuleEntity entity = new GatewayFlowRuleEntity(); entity.setId(1L); entity.setApp(TEST_APP); entity.setIp(TEST_IP); entity.setPort(TEST_PORT); entity.setResource("httpbin_route"); entity.setResourceMode(RESOURCE_MODE_ROUTE_ID); entity.setGrade(FLOW_GRADE_QPS); entity.setCount(5D); entity.setInterval(30L); entity.setIntervalUnit(GatewayFlowRuleEntity.INTERVAL_UNIT_SECOND); entity.setControlBehavior(CONTROL_BEHAVIOR_DEFAULT); entity.setBurst(0); entity.setMaxQueueingTimeoutMs(0); GatewayParamFlowItemEntity itemEntity = new GatewayParamFlowItemEntity(); entity.setParamItem(itemEntity); itemEntity.setParseStrategy(PARAM_PARSE_STRATEGY_CLIENT_IP); entities.add(entity); GatewayFlowRuleEntity entity2 = new GatewayFlowRuleEntity(); entity2.setId(2L); entity2.setApp(TEST_APP); entity2.setIp(TEST_IP); entity2.setPort(TEST_PORT); entity2.setResource("some_customized_api"); entity2.setResourceMode(RESOURCE_MODE_CUSTOM_API_NAME); entity2.setCount(30D); entity2.setInterval(2L); entity2.setIntervalUnit(GatewayFlowRuleEntity.INTERVAL_UNIT_MINUTE); entity2.setControlBehavior(CONTROL_BEHAVIOR_DEFAULT); entity2.setBurst(0); entity2.setMaxQueueingTimeoutMs(0); GatewayParamFlowItemEntity itemEntity2 = new GatewayParamFlowItemEntity(); entity2.setParamItem(itemEntity2); itemEntity2.setParseStrategy(PARAM_PARSE_STRATEGY_CLIENT_IP); entities.add(entity2); CompletableFuture<List<GatewayFlowRuleEntity>> completableFuture = mock(CompletableFuture.class); given(completableFuture.get()).willReturn(entities); given(sentinelApiClient.fetchGatewayFlowRules(TEST_APP, TEST_IP, TEST_PORT)).willReturn(completableFuture); MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get(path); requestBuilder.param("app", TEST_APP); requestBuilder.param("ip", TEST_IP); requestBuilder.param("port", String.valueOf(TEST_PORT)); // Do controller logic MvcResult mvcResult = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.status().isOk()).andDo(MockMvcResultHandlers.print()).andReturn(); // Verify the fetchGatewayFlowRules method has been called verify(sentinelApiClient).fetchGatewayFlowRules(TEST_APP, TEST_IP, TEST_PORT); // Verify if two same entities are got Result<List<GatewayFlowRuleEntity>> result = JSONObject.parseObject(mvcResult.getResponse().getContentAsString(), new TypeReference<Result<List<GatewayFlowRuleEntity>>>(){}); assertTrue(result.isSuccess()); List<GatewayFlowRuleEntity> data = result.getData(); assertEquals(2, data.size()); assertEquals(entities, data); // Verify the entities are add into memory repository List<GatewayFlowRuleEntity> entitiesInMem = repository.findAllByApp(TEST_APP); assertEquals(2, entitiesInMem.size()); assertEquals(entities, entitiesInMem); }
Example 14
Source File: GatewayApiControllerTest.java From Sentinel with Apache License 2.0 | 4 votes |
@Test public void testQueryApis() throws Exception { String path = "/gateway/api/list.json"; List<ApiDefinitionEntity> entities = new ArrayList<>(); // Mock two entities ApiDefinitionEntity entity = new ApiDefinitionEntity(); entity.setId(1L); entity.setApp(TEST_APP); entity.setIp(TEST_IP); entity.setPort(TEST_PORT); entity.setApiName("foo"); Date date = new Date(); entity.setGmtCreate(date); entity.setGmtModified(date); Set<ApiPredicateItemEntity> itemEntities = new LinkedHashSet<>(); entity.setPredicateItems(itemEntities); ApiPredicateItemEntity itemEntity = new ApiPredicateItemEntity(); itemEntity.setPattern("/aaa"); itemEntity.setMatchStrategy(URL_MATCH_STRATEGY_EXACT); itemEntities.add(itemEntity); entities.add(entity); ApiDefinitionEntity entity2 = new ApiDefinitionEntity(); entity2.setId(2L); entity2.setApp(TEST_APP); entity2.setIp(TEST_IP); entity2.setPort(TEST_PORT); entity2.setApiName("biz"); entity.setGmtCreate(date); entity.setGmtModified(date); Set<ApiPredicateItemEntity> itemEntities2 = new LinkedHashSet<>(); entity2.setPredicateItems(itemEntities2); ApiPredicateItemEntity itemEntity2 = new ApiPredicateItemEntity(); itemEntity2.setPattern("/bbb"); itemEntity2.setMatchStrategy(URL_MATCH_STRATEGY_PREFIX); itemEntities2.add(itemEntity2); entities.add(entity2); CompletableFuture<List<ApiDefinitionEntity>> completableFuture = mock(CompletableFuture.class); given(completableFuture.get()).willReturn(entities); given(sentinelApiClient.fetchApis(TEST_APP, TEST_IP, TEST_PORT)).willReturn(completableFuture); MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get(path); requestBuilder.param("app", TEST_APP); requestBuilder.param("ip", TEST_IP); requestBuilder.param("port", String.valueOf(TEST_PORT)); // Do controller logic MvcResult mvcResult = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.status().isOk()).andDo(MockMvcResultHandlers.print()).andReturn(); // Verify the fetchApis method has been called verify(sentinelApiClient).fetchApis(TEST_APP, TEST_IP, TEST_PORT); // Verify if two same entities are got Result<List<ApiDefinitionEntity>> result = JSONObject.parseObject(mvcResult.getResponse().getContentAsString(), new TypeReference<Result<List<ApiDefinitionEntity>>>(){}); assertTrue(result.isSuccess()); List<ApiDefinitionEntity> data = result.getData(); assertEquals(2, data.size()); assertEquals(entities, data); // Verify the entities are add into memory repository List<ApiDefinitionEntity> entitiesInMem = repository.findAllByApp(TEST_APP); assertEquals(2, entitiesInMem.size()); assertEquals(entities, entitiesInMem); }
Example 15
Source File: NotificationControllTest.java From rebuild with GNU General Public License v3.0 | 4 votes |
@Test public void testPageIndex() throws Exception { MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/notifications"); System.out.println(perform(builder, UserService.ADMIN_USER)); }
Example 16
Source File: DapTestCommon.java From tds with BSD 3-Clause "New" or "Revised" License | 4 votes |
public HttpResponse execute(HttpRequestBase rq) throws IOException { URI uri = rq.getURI(); DapController controller = getController(uri); StandaloneMockMvcBuilder mvcbuilder = MockMvcBuilders.standaloneSetup(controller); mvcbuilder.setValidator(new TestServlet.NullValidator()); MockMvc mockMvc = mvcbuilder.build(); MockHttpServletRequestBuilder mockrb = MockMvcRequestBuilders.get(uri); // We need to use only the path part mockrb.servletPath(uri.getPath()); // Move any headers from rq to mockrb Header[] headers = rq.getAllHeaders(); for (int i = 0; i < headers.length; i++) { Header h = headers[i]; mockrb.header(h.getName(), h.getValue()); } // Since the url has the query parameters, // they will automatically be parsed and added // to the rb parameters. // Finally set the resource dir mockrb.requestAttr("RESOURCEDIR", this.resourcepath); // Now invoke the servlet MvcResult result; try { result = mockMvc.perform(mockrb).andReturn(); } catch (Exception e) { throw new IOException(e); } // Collect the output MockHttpServletResponse res = result.getResponse(); byte[] byteresult = res.getContentAsByteArray(); // Convert to HttpResponse HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, res.getStatus(), ""); if (response == null) throw new IOException("HTTPMethod.executeMock: Response was null"); Collection<String> keys = res.getHeaderNames(); // Move headers to the response for (String key : keys) { List<String> values = res.getHeaders(key); for (String v : values) { response.addHeader(key, v); } } ByteArrayEntity entity = new ByteArrayEntity(byteresult); String sct = res.getContentType(); entity.setContentType(sct); response.setEntity(entity); return response; }
Example 17
Source File: DemoApplicationTests.java From verifydata with Apache License 2.0 | 4 votes |
@Test public void testParmPathVariable() throws Exception { RequestBuilder request = MockMvcRequestBuilders.get("/gets/1888/end"); MvcResult mvcResult = mockMvc.perform(request).andReturn(); System.out.println(new String(mvcResult.getResponse().getContentAsString().getBytes(), "utf-8")); }
Example 18
Source File: GatewayFlowRuleControllerTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 4 votes |
@Test public void testQueryFlowRules() throws Exception { String path = "/gateway/flow/list.json"; List<GatewayFlowRuleEntity> entities = new ArrayList<>(); // Mock two entities GatewayFlowRuleEntity entity = new GatewayFlowRuleEntity(); entity.setId(1L); entity.setApp(TEST_APP); entity.setIp(TEST_IP); entity.setPort(TEST_PORT); entity.setResource("httpbin_route"); entity.setResourceMode(RESOURCE_MODE_ROUTE_ID); entity.setGrade(FLOW_GRADE_QPS); entity.setCount(5D); entity.setInterval(30L); entity.setIntervalUnit(GatewayFlowRuleEntity.INTERVAL_UNIT_SECOND); entity.setControlBehavior(CONTROL_BEHAVIOR_DEFAULT); entity.setBurst(0); entity.setMaxQueueingTimeoutMs(0); GatewayParamFlowItemEntity itemEntity = new GatewayParamFlowItemEntity(); entity.setParamItem(itemEntity); itemEntity.setParseStrategy(PARAM_PARSE_STRATEGY_CLIENT_IP); entities.add(entity); GatewayFlowRuleEntity entity2 = new GatewayFlowRuleEntity(); entity2.setId(2L); entity2.setApp(TEST_APP); entity2.setIp(TEST_IP); entity2.setPort(TEST_PORT); entity2.setResource("some_customized_api"); entity2.setResourceMode(RESOURCE_MODE_CUSTOM_API_NAME); entity2.setCount(30D); entity2.setInterval(2L); entity2.setIntervalUnit(GatewayFlowRuleEntity.INTERVAL_UNIT_MINUTE); entity2.setControlBehavior(CONTROL_BEHAVIOR_DEFAULT); entity2.setBurst(0); entity2.setMaxQueueingTimeoutMs(0); GatewayParamFlowItemEntity itemEntity2 = new GatewayParamFlowItemEntity(); entity2.setParamItem(itemEntity2); itemEntity2.setParseStrategy(PARAM_PARSE_STRATEGY_CLIENT_IP); entities.add(entity2); CompletableFuture<List<GatewayFlowRuleEntity>> completableFuture = mock(CompletableFuture.class); given(completableFuture.get()).willReturn(entities); given(sentinelApiClient.fetchGatewayFlowRules(TEST_APP, TEST_IP, TEST_PORT)).willReturn(completableFuture); MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get(path); requestBuilder.param("app", TEST_APP); requestBuilder.param("ip", TEST_IP); requestBuilder.param("port", String.valueOf(TEST_PORT)); // Do controller logic MvcResult mvcResult = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.status().isOk()).andDo(MockMvcResultHandlers.print()).andReturn(); // Verify the fetchGatewayFlowRules method has been called verify(sentinelApiClient).fetchGatewayFlowRules(TEST_APP, TEST_IP, TEST_PORT); // Verify if two same entities are got Result<List<GatewayFlowRuleEntity>> result = JSONObject.parseObject(mvcResult.getResponse().getContentAsString(), new TypeReference<Result<List<GatewayFlowRuleEntity>>>(){}); assertTrue(result.isSuccess()); List<GatewayFlowRuleEntity> data = result.getData(); assertEquals(2, data.size()); assertEquals(entities, data); // Verify the entities are add into memory repository List<GatewayFlowRuleEntity> entitiesInMem = repository.findAllByApp(TEST_APP); assertEquals(2, entitiesInMem.size()); assertEquals(entities, entitiesInMem); }
Example 19
Source File: GatewayApiControllerTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 4 votes |
@Test public void testQueryApis() throws Exception { String path = "/gateway/api/list.json"; List<ApiDefinitionEntity> entities = new ArrayList<>(); // Mock two entities ApiDefinitionEntity entity = new ApiDefinitionEntity(); entity.setId(1L); entity.setApp(TEST_APP); entity.setIp(TEST_IP); entity.setPort(TEST_PORT); entity.setApiName("foo"); Date date = new Date(); entity.setGmtCreate(date); entity.setGmtModified(date); Set<ApiPredicateItemEntity> itemEntities = new LinkedHashSet<>(); entity.setPredicateItems(itemEntities); ApiPredicateItemEntity itemEntity = new ApiPredicateItemEntity(); itemEntity.setPattern("/aaa"); itemEntity.setMatchStrategy(URL_MATCH_STRATEGY_EXACT); itemEntities.add(itemEntity); entities.add(entity); ApiDefinitionEntity entity2 = new ApiDefinitionEntity(); entity2.setId(2L); entity2.setApp(TEST_APP); entity2.setIp(TEST_IP); entity2.setPort(TEST_PORT); entity2.setApiName("biz"); entity.setGmtCreate(date); entity.setGmtModified(date); Set<ApiPredicateItemEntity> itemEntities2 = new LinkedHashSet<>(); entity2.setPredicateItems(itemEntities2); ApiPredicateItemEntity itemEntity2 = new ApiPredicateItemEntity(); itemEntity2.setPattern("/bbb"); itemEntity2.setMatchStrategy(URL_MATCH_STRATEGY_PREFIX); itemEntities2.add(itemEntity2); entities.add(entity2); CompletableFuture<List<ApiDefinitionEntity>> completableFuture = mock(CompletableFuture.class); given(completableFuture.get()).willReturn(entities); given(sentinelApiClient.fetchApis(TEST_APP, TEST_IP, TEST_PORT)).willReturn(completableFuture); MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get(path); requestBuilder.param("app", TEST_APP); requestBuilder.param("ip", TEST_IP); requestBuilder.param("port", String.valueOf(TEST_PORT)); // Do controller logic MvcResult mvcResult = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.status().isOk()).andDo(MockMvcResultHandlers.print()).andReturn(); // Verify the fetchApis method has been called verify(sentinelApiClient).fetchApis(TEST_APP, TEST_IP, TEST_PORT); // Verify if two same entities are got Result<List<ApiDefinitionEntity>> result = JSONObject.parseObject(mvcResult.getResponse().getContentAsString(), new TypeReference<Result<List<ApiDefinitionEntity>>>(){}); assertTrue(result.isSuccess()); List<ApiDefinitionEntity> data = result.getData(); assertEquals(2, data.size()); assertEquals(entities, data); // Verify the entities are add into memory repository List<ApiDefinitionEntity> entitiesInMem = repository.findAllByApp(TEST_APP); assertEquals(2, entitiesInMem.size()); assertEquals(entities, entitiesInMem); }
Example 20
Source File: ChartDesignControllTest.java From rebuild with GNU General Public License v3.0 | 4 votes |
@Test public void testDesignPage() throws Exception { MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/dashboard/chart-design?source=LoginLog"); System.out.println(perform(builder, UserService.ADMIN_USER)); }