LineBotCallbackRequestParser { public CallbackRequest handle(HttpServletRequest req) throws LineBotCallbackException, IOException { final String signature = req.getHeader(WebhookParser.SIGNATURE_HEADER_NAME); final byte[] json = ByteStreams.toByteArray(req.getInputStream()); try { return parser.handle(signature, json); } catch (WebhookParseException e) { throw new LineBotCallbackException(e.getMessage(), e); } } LineBotCallbackRequestParser(@NonNull LineSignatureValidator lineSignatureValidator); CallbackRequest handle(HttpServletRequest req);  }
@Test public void testMissingHeader() { final MockHttpServletRequest request = new MockHttpServletRequest(); assertThatThrownBy(() -> lineBotCallbackRequestParser.handle(request)) .isInstanceOf(LineBotCallbackException.class) .hasMessage("Missing 'X-Line-Signature' header"); }
@Test public void testInvalidSignature() { final MockHttpServletRequest request = new MockHttpServletRequest(); request.addHeader("X-Line-Signature", "SSSSIGNATURE"); request.setContent("{}".getBytes(StandardCharsets.UTF_8)); assertThatThrownBy(() -> lineBotCallbackRequestParser.handle(request)) .isInstanceOf(LineBotCallbackException.class) .hasMessage("Invalid API signature"); }
@Test public void testNullRequest() { final byte[] requestBody = "null".getBytes(StandardCharsets.UTF_8); final MockHttpServletRequest request = new MockHttpServletRequest(); request.addHeader("X-Line-Signature", "SSSSIGNATURE"); request.setContent(requestBody); doReturn(true).when(lineSignatureValidator).validateSignature(requestBody, "SSSSIGNATURE"); assertThatThrownBy(() -> lineBotCallbackRequestParser.handle(request)) .isInstanceOf(LineBotCallbackException.class) .hasMessage("Invalid content"); }
@Test public void testCallRequest() throws Exception { final InputStream resource = getClass().getClassLoader().getResourceAsStream("callback-request.json"); final byte[] requestBody = ByteStreams.toByteArray(resource); MockHttpServletRequest request = new MockHttpServletRequest(); request.addHeader("X-Line-Signature", "SSSSIGNATURE"); request.setContent(requestBody); doReturn(true).when(lineSignatureValidator).validateSignature(requestBody, "SSSSIGNATURE"); final CallbackRequest callbackRequest = lineBotCallbackRequestParser.handle(request); assertThat(callbackRequest).isNotNull(); final List<Event> result = callbackRequest.getEvents(); final MessageEvent messageEvent = (MessageEvent) result.get(0); final TextMessageContent text = (TextMessageContent) messageEvent.getMessage(); assertThat(text.getText()).isEqualTo("Hello, world"); final String followedUserId = messageEvent.getSource().getUserId(); assertThat(followedUserId).isEqualTo("u206d25c2ea6bd87c17655609a1c37cb8"); assertThat(messageEvent.getTimestamp()).isEqualTo(Instant.parse("2016-05-07T13:57:59.859Z")); }