diff options
author | Vitaly Takmazov | 2019-04-07 01:59:33 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2019-04-07 01:59:33 +0300 |
commit | e359e0788d4d9c675a88daaebda416f38e2ac03a (patch) | |
tree | 379cccea18d81ae56036dcb536c8a75c237ae43b /src/test | |
parent | 384c61ceae3301c6bc92ee6f591ed9d186b15204 (diff) |
Tags should be unescaped before storing
Test tag is Test tag
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/java/com/juick/server/tests/ServerTests.java | 58 |
1 files changed, 31 insertions, 27 deletions
diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java index afe7f659..65e19b89 100644 --- a/src/test/java/com/juick/server/tests/ServerTests.java +++ b/src/test/java/com/juick/server/tests/ServerTests.java @@ -1028,22 +1028,32 @@ public class ServerTests { assertThat(tags.get(0).getName(), equalTo("yo")); } @Test - public void messageParserSerializer() throws ParserConfigurationException, - IOException, SAXException, JAXBException { - Set<Tag> tags = MessageUtils.parseTags("test test" + (char) 0xA0 + "2 test3"); - List<Tag> tagList = new ArrayList<>(tags); + public void messageParserSerializer() throws Exception { + String tagsString = "test test" + (char) 0xA0 + "2 test 3"; + Set<Tag> tags = MessageUtils.parseTags(tagsString); + List<Tag> tagList = tags.stream().map(t -> tagService.getTag(t.getName(), true)) + .collect(Collectors.toList()); assertEquals("First tag must be", "test", tagList.get(0).getName()); - assertEquals("Third tag must be", "test3", tagList.get(2).getName()); + assertEquals("Third tag must be", "test 3", tagList.get(2).getName()); assertEquals("Count of tags must be", 3, tagList.size()); - Message msg = new Message(); - msg.setTags(tags); - Instant currentDate = Instant.now(); - msg.setCreated(currentDate); + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); + MultiValueMap<String, String> map = new LinkedMultiValueMap<>(); + HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers); + + map.add("body", "*test *test 2 *test 3 YO"); + map.add("hash", userService.getHashByUID(ugnich.getUid())); + ResponseEntity<CommandResult> result = restTemplate.postForEntity( + "/api/post", + request, CommandResult.class); + assertThat(result.getStatusCode(), is(HttpStatus.OK)); + Message msg = result.getBody().getNewMessage().orElseThrow(); + Instant currentDate = msg.getCreated(); String jsonMessage = jsonMapper.writeValueAsString(msg); - assertEquals("date should be in timestamp field", DateFormattersHolder.getMessageFormatterInstance().format(currentDate), + assertEquals("date should be in timestamp field", + DateFormattersHolder.getMessageFormatterInstance().format(currentDate), JsonPath.read(jsonMessage, "$.timestamp")); - JAXBContext context = JAXBContext .newInstance(Message.class); Marshaller m = context.createMarshaller(); @@ -1056,22 +1066,16 @@ public class ServerTests { Document doc = db.parse(new ByteArrayInputStream(sw.toString().getBytes(StandardCharsets.UTF_8))); Node juickNode = doc.getDocumentElement(); NamedNodeMap attrs = juickNode.getAttributes(); - assertEquals("date should be in ts field", DateFormattersHolder.getMessageFormatterInstance().format(currentDate), + assertEquals("date should be in ts field", + DateFormattersHolder.getMessageFormatterInstance().format(currentDate), attrs.getNamedItem("ts").getNodeValue()); - } - @Test - public void restTemplateTests() { - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); - MultiValueMap<String, String> map= new LinkedMultiValueMap<>(); - HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers); - map.add("body", "yo"); - map.add("hash", userService.getHashByUID(ugnich.getUid())); - ResponseEntity<CommandResult> result = restTemplate.postForEntity( - "/api/post", - request, CommandResult.class); - assertThat(result.getStatusCode(), is(HttpStatus.OK)); + MvcResult apiResult = mockMvc.perform(get("/api/thread?mid=" + msg.getMid())) + .andExpect(status().isOk()) + .andReturn(); + List<Message> fromApi = jsonMapper.readValue(apiResult.getResponse().getContentAsString(), + new TypeReference<List<Message>>() {}); + assertThat(fromApi.get(0).getTags(), is(tags)); } @Test public void emptyAuthenticatedPostShouldThrowBadRequest() throws Exception { @@ -1486,9 +1490,9 @@ public class ServerTests { Writer writer = new StringWriter(); template.evaluate(writer, Collections.singletonMap("tagsList", - Collections.singletonList(StringEscapeUtils.escapeHtml4(new Tag(">_<").getName())))); + Collections.singletonList(new Tag(">_<").getName()))); String output = writer.toString().trim(); - assertThat(output, equalTo("<a href=\"/ugnich/?tag=%26gt%3B_%26lt%3B\">>_<</a>")); + assertThat(output, equalTo("<a href=\"/ugnich/?tag=%3E_%3C\">>_<</a>")); } public DomElement fetchMeta(String url, String name) throws IOException { |