Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
/**
* Created by joelz on 8/6/15.
*
*
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
Expand All @@ -19,34 +16,36 @@
*/
package org.apache.zeppelin.server;

import org.apache.zeppelin.socket.TestHttpServletRequest;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.*;

/**
* BASIC Cors rest api tests
* Basic CORS REST API tests
*/
public class CorsFilterTest {

public static String[] headers = new String[8];
public static Integer count = 0;

@Test
@SuppressWarnings("rawtypes")
public void ValidCorsFilterTest() throws IOException, ServletException {
CorsFilter filter = new CorsFilter();
HttpServletResponse mockResponse = mock(HttpServletResponse.class);
FilterChain mockedFilterChain = mock(FilterChain.class);
TestHttpServletRequest mockRequest = mock(TestHttpServletRequest.class);
HttpServletRequest mockRequest = mock(HttpServletRequest.class);
when(mockRequest.getHeader("Origin")).thenReturn("http://localhost:8080");
when(mockRequest.getMethod()).thenReturn("Empty");
when(mockRequest.getServerName()).thenReturn("localhost");
Expand All @@ -66,11 +65,12 @@ public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
}

@Test
@SuppressWarnings("rawtypes")
public void InvalidCorsFilterTest() throws IOException, ServletException {
CorsFilter filter = new CorsFilter();
HttpServletResponse mockResponse = mock(HttpServletResponse.class);
FilterChain mockedFilterChain = mock(FilterChain.class);
TestHttpServletRequest mockRequest = mock(TestHttpServletRequest.class);
HttpServletRequest mockRequest = mock(HttpServletRequest.class);
when(mockRequest.getHeader("Origin")).thenReturn("http://evillocalhost:8080");
when(mockRequest.getMethod()).thenReturn("Empty");
when(mockRequest.getServerName()).thenReturn("evillocalhost");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
/**
* Created by joelz on 8/6/15.
*
*
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
Expand Down Expand Up @@ -35,10 +32,12 @@
import org.apache.zeppelin.rest.AbstractTestRestApi;
import org.apache.zeppelin.server.ZeppelinServer;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import javax.servlet.http.HttpServletRequest;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
Expand All @@ -51,12 +50,13 @@


/**
* BASIC Zeppelin rest api tests
* Basic REST API tests for notebookServer
*/
public class NotebookServerTest extends AbstractTestRestApi {
private static Notebook notebook;
private static NotebookServer notebookServer;
private static Gson gson;
private HttpServletRequest mockRequest;

@BeforeClass
public static void init() throws Exception {
Expand All @@ -71,19 +71,24 @@ public static void destroy() throws Exception {
AbstractTestRestApi.shutDown();
}

@Before
public void setUp() {
mockRequest = mock(HttpServletRequest.class);
}

@Test
public void checkOrigin() throws UnknownHostException {
NotebookServer server = new NotebookServer();
String origin = "http://" + InetAddress.getLocalHost().getHostName() + ":8080";

assertTrue("Origin " + origin + " is not allowed. Please check your hostname.",
server.checkOrigin(new TestHttpServletRequest(), origin));
server.checkOrigin(mockRequest, origin));
}

@Test
public void checkInvalidOrigin(){
NotebookServer server = new NotebookServer();
assertFalse(server.checkOrigin(new TestHttpServletRequest(), "http://evillocalhost:8080"));
assertFalse(server.checkOrigin(mockRequest, "http://evillocalhost:8080"));
}

@Test
Expand Down Expand Up @@ -166,7 +171,7 @@ public void testImportNotebook() throws IOException {
}

@Test
public void should_bind_angular_object_to_remote_for_paragraphs() throws Exception {
public void bindAngularObjectToRemoteForParagraphs() throws Exception {
//Given
final String varName = "name";
final String value = "DuyHai DOAN";
Expand All @@ -191,8 +196,7 @@ public void should_bind_angular_object_to_remote_for_paragraphs() throws Excepti

when(paragraph.getCurrentRepl().getInterpreterGroup()).thenReturn(mdGroup);


final AngularObject ao1 = AngularObjectBuilder.build(varName, value, "noteId", "paragraphId");
final AngularObject<String> ao1 = AngularObjectBuilder.build(varName, value, "noteId", "paragraphId");

when(mdRegistry.addAndNotifyRemoteProcess(varName, value, "noteId", "paragraphId")).thenReturn(ao1);

Expand All @@ -217,7 +221,7 @@ public void should_bind_angular_object_to_remote_for_paragraphs() throws Excepti
}

@Test
public void should_bind_angular_object_to_local_for_paragraphs() throws Exception {
public void bindAngularObjectToLocalForParagraphs() throws Exception {
//Given
final String varName = "name";
final String value = "DuyHai DOAN";
Expand All @@ -241,7 +245,7 @@ public void should_bind_angular_object_to_local_for_paragraphs() throws Exceptio
when(paragraph.getCurrentRepl().getInterpreterGroup()).thenReturn(mdGroup);


final AngularObject ao1 = AngularObjectBuilder.build(varName, value, "noteId", "paragraphId");
final AngularObject<String> ao1 = AngularObjectBuilder.build(varName, value, "noteId", "paragraphId");

when(mdRegistry.add(varName, value, "noteId", "paragraphId")).thenReturn(ao1);

Expand All @@ -264,7 +268,7 @@ public void should_bind_angular_object_to_local_for_paragraphs() throws Exceptio
}

@Test
public void should_unbind_angular_object_from_remote_for_paragraphs() throws Exception {
public void unbindAngularObjectFromRemoteForParagraphs() throws Exception {
//Given
final String varName = "name";
final String value = "val";
Expand All @@ -286,7 +290,7 @@ public void should_unbind_angular_object_from_remote_for_paragraphs() throws Exc

when(paragraph.getCurrentRepl().getInterpreterGroup()).thenReturn(mdGroup);

final AngularObject ao1 = AngularObjectBuilder.build(varName, value, "noteId", "paragraphId");
final AngularObject<String> ao1 = AngularObjectBuilder.build(varName, value, "noteId", "paragraphId");
when(mdRegistry.removeAndNotifyRemoteProcess(varName, "noteId", "paragraphId")).thenReturn(ao1);
NotebookSocket conn = mock(NotebookSocket.class);
NotebookSocket otherConn = mock(NotebookSocket.class);
Expand All @@ -309,7 +313,7 @@ public void should_unbind_angular_object_from_remote_for_paragraphs() throws Exc
}

@Test
public void should_unbind_angular_object_from_local_for_paragraphs() throws Exception {
public void unbindAngularObjectFromLocalForParagraphs() throws Exception {
//Given
final String varName = "name";
final String value = "val";
Expand All @@ -331,8 +335,7 @@ public void should_unbind_angular_object_from_local_for_paragraphs() throws Exce

when(paragraph.getCurrentRepl().getInterpreterGroup()).thenReturn(mdGroup);

final AngularObject ao1 = AngularObjectBuilder.build(varName, value, "noteId", "paragraphId");

final AngularObject<String> ao1 = AngularObjectBuilder.build(varName, value, "noteId", "paragraphId");

when(mdRegistry.remove(varName, "noteId", "paragraphId")).thenReturn(ao1);

Expand All @@ -355,12 +358,9 @@ public void should_unbind_angular_object_from_local_for_paragraphs() throws Exce

private NotebookSocket createWebSocket() {
NotebookSocket sock = mock(NotebookSocket.class);
when(sock.getRequest()).thenReturn(createHttpServletRequest());
when(sock.getRequest()).thenReturn(mockRequest);
return sock;
}

private HttpServletRequest createHttpServletRequest() {
return mock(HttpServletRequest.class);
}
}

Loading