Skip to content

Commit 622185a

Browse files
author
Paul Gray
committed
Adding tests for LtiValidator/LtiSigner
1 parent 7216de4 commit 622185a

File tree

4 files changed

+447
-0
lines changed

4 files changed

+447
-0
lines changed
Lines changed: 330 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,330 @@
1+
package org.imsglobal.lti.launch;
2+
3+
4+
import org.apache.http.Header;
5+
import org.apache.http.HttpRequest;
6+
7+
import javax.servlet.RequestDispatcher;
8+
import javax.servlet.ServletInputStream;
9+
import javax.servlet.http.Cookie;
10+
import javax.servlet.http.HttpServletRequest;
11+
import javax.servlet.http.HttpSession;
12+
import java.io.BufferedReader;
13+
import java.io.IOException;
14+
import java.io.UnsupportedEncodingException;
15+
import java.net.URI;
16+
import java.security.Principal;
17+
import java.util.*;
18+
19+
/**
20+
* Created by paul on 8/24/16.
21+
*/
22+
public abstract class BaseMockHttpServletRequest implements HttpServletRequest {
23+
24+
HttpRequest req;
25+
26+
public BaseMockHttpServletRequest(HttpRequest req) {
27+
this.req = req;
28+
}
29+
30+
@Override
31+
public String getAuthType() {
32+
throw new UnsupportedOperationException("unsupported");
33+
}
34+
35+
@Override
36+
public Cookie[] getCookies() {
37+
throw new UnsupportedOperationException("unsupported");
38+
}
39+
40+
@Override
41+
public long getDateHeader(String name) {
42+
throw new UnsupportedOperationException("unsupported");
43+
}
44+
45+
@Override
46+
public String getHeader(String name) {
47+
throw new UnsupportedOperationException("unsupported");
48+
}
49+
50+
@Override
51+
public Enumeration getHeaders(String name) {
52+
List<String> headerStrs = new ArrayList<>();
53+
for(Header hdr: Arrays.asList(req.getHeaders(name))){
54+
headerStrs.add(hdr.getValue());
55+
}
56+
return Collections.enumeration(headerStrs);
57+
}
58+
59+
@Override
60+
public Enumeration getHeaderNames() {
61+
List<String> headerNames = new LinkedList<>();
62+
for(Header hdr: req.getAllHeaders()) {
63+
headerNames.add(hdr.getName());
64+
}
65+
return Collections.enumeration(headerNames);
66+
}
67+
68+
@Override
69+
public int getIntHeader(String name) {
70+
throw new UnsupportedOperationException("unsupported");
71+
}
72+
73+
@Override
74+
public String getMethod() {
75+
throw new UnsupportedOperationException("unsupported");
76+
}
77+
78+
@Override
79+
public String getPathInfo() {
80+
throw new UnsupportedOperationException("unsupported");
81+
}
82+
83+
@Override
84+
public String getPathTranslated() {
85+
throw new UnsupportedOperationException("unsupported");
86+
}
87+
88+
@Override
89+
public String getContextPath() {
90+
throw new UnsupportedOperationException("unsupported");
91+
}
92+
93+
@Override
94+
public String getQueryString() {
95+
try {
96+
String q = new URI(req.getRequestLine().getUri()).getQuery();
97+
return q;
98+
} catch(Exception e) {
99+
throw new RuntimeException(e);
100+
}
101+
}
102+
103+
@Override
104+
public String getRemoteUser() {
105+
throw new UnsupportedOperationException("unsupported");
106+
}
107+
108+
@Override
109+
public boolean isUserInRole(String role) {
110+
throw new UnsupportedOperationException("unsupported");
111+
}
112+
113+
@Override
114+
public Principal getUserPrincipal() {
115+
throw new UnsupportedOperationException("unsupported");
116+
}
117+
118+
@Override
119+
public String getRequestedSessionId() {
120+
throw new UnsupportedOperationException("unsupported");
121+
}
122+
123+
@Override
124+
public String getRequestURI() {
125+
throw new UnsupportedOperationException("unsupported");
126+
}
127+
128+
@Override
129+
public StringBuffer getRequestURL() {
130+
try {
131+
String url = req.getRequestLine().getUri().replaceFirst("\\?(.*)", "");
132+
return new StringBuffer(url);
133+
} catch (Exception e) {
134+
throw new RuntimeException(e);
135+
}
136+
}
137+
138+
@Override
139+
public String getServletPath() {
140+
throw new UnsupportedOperationException("unsupported");
141+
}
142+
143+
@Override
144+
public HttpSession getSession(boolean create) {
145+
throw new UnsupportedOperationException("unsupported");
146+
}
147+
148+
@Override
149+
public HttpSession getSession() {
150+
throw new UnsupportedOperationException("unsupported");
151+
}
152+
153+
@Override
154+
public boolean isRequestedSessionIdValid() {
155+
throw new UnsupportedOperationException("unsupported");
156+
}
157+
158+
@Override
159+
public boolean isRequestedSessionIdFromCookie() {
160+
throw new UnsupportedOperationException("unsupported");
161+
}
162+
163+
@Override
164+
public boolean isRequestedSessionIdFromURL() {
165+
throw new UnsupportedOperationException("unsupported");
166+
}
167+
168+
@Override
169+
public boolean isRequestedSessionIdFromUrl() {
170+
throw new UnsupportedOperationException("unsupported");
171+
}
172+
173+
@Override
174+
public Object getAttribute(String name) {
175+
throw new UnsupportedOperationException("unsupported");
176+
}
177+
178+
@Override
179+
public Enumeration getAttributeNames() {
180+
throw new UnsupportedOperationException("unsupported");
181+
}
182+
183+
@Override
184+
public String getCharacterEncoding() {
185+
throw new UnsupportedOperationException("unsupported");
186+
}
187+
188+
@Override
189+
public void setCharacterEncoding(String env) throws UnsupportedEncodingException {
190+
191+
}
192+
193+
@Override
194+
public int getContentLength() {
195+
throw new UnsupportedOperationException("unsupported");
196+
}
197+
198+
@Override
199+
public String getContentType() {
200+
throw new UnsupportedOperationException("unsupported");
201+
}
202+
203+
@Override
204+
public ServletInputStream getInputStream() throws IOException {
205+
throw new UnsupportedOperationException("unsupported");
206+
}
207+
208+
@Override
209+
public String getParameter(String name) {
210+
return (String) this.getParameterMap().get(name);
211+
}
212+
213+
@Override
214+
public Enumeration getParameterNames() {
215+
throw new UnsupportedOperationException("unsupported");
216+
}
217+
218+
@Override
219+
public String[] getParameterValues(String name) {
220+
throw new UnsupportedOperationException("unsupported");
221+
}
222+
223+
@Override
224+
public Map getParameterMap() {
225+
throw new UnsupportedOperationException("unsupported");
226+
}
227+
228+
@Override
229+
public String getProtocol() {
230+
throw new UnsupportedOperationException("unsupported");
231+
}
232+
233+
@Override
234+
public String getScheme() {
235+
throw new UnsupportedOperationException("unsupported");
236+
}
237+
238+
@Override
239+
public String getServerName() {
240+
throw new UnsupportedOperationException("unsupported");
241+
}
242+
243+
@Override
244+
public int getServerPort() {
245+
throw new UnsupportedOperationException("unsupported");
246+
}
247+
248+
@Override
249+
public BufferedReader getReader() throws IOException {
250+
throw new UnsupportedOperationException("unsupported");
251+
}
252+
253+
@Override
254+
public String getRemoteAddr() {
255+
throw new UnsupportedOperationException("unsupported");
256+
}
257+
258+
@Override
259+
public String getRemoteHost() {
260+
throw new UnsupportedOperationException("unsupported");
261+
}
262+
263+
@Override
264+
public void setAttribute(String name, Object o) {
265+
266+
}
267+
268+
@Override
269+
public void removeAttribute(String name) {
270+
271+
}
272+
273+
@Override
274+
public Locale getLocale() {
275+
throw new UnsupportedOperationException("unsupported");
276+
}
277+
278+
@Override
279+
public Enumeration getLocales() {
280+
throw new UnsupportedOperationException("unsupported");
281+
}
282+
283+
@Override
284+
public boolean isSecure() {
285+
throw new UnsupportedOperationException("unsupported");
286+
}
287+
288+
@Override
289+
public RequestDispatcher getRequestDispatcher(String path) {
290+
throw new UnsupportedOperationException("unsupported");
291+
}
292+
293+
@Override
294+
public String getRealPath(String path) {
295+
throw new UnsupportedOperationException("unsupported");
296+
}
297+
298+
@Override
299+
public int getRemotePort() {
300+
throw new UnsupportedOperationException("unsupported");
301+
}
302+
303+
@Override
304+
public String getLocalName() {
305+
throw new UnsupportedOperationException("unsupported");
306+
}
307+
308+
@Override
309+
public String getLocalAddr() {
310+
throw new UnsupportedOperationException("unsupported");
311+
}
312+
313+
@Override
314+
public int getLocalPort() {
315+
throw new UnsupportedOperationException("unsupported");
316+
}
317+
318+
protected static Map<String, String> getQueryMap(String query)
319+
{
320+
String[] params = query.split("&");
321+
Map<String, String> map = new HashMap<String, String>();
322+
for (String param : params)
323+
{
324+
String name = param.split("=")[0];
325+
String value = param.split("=")[1];
326+
map.put(name, value);
327+
}
328+
return map;
329+
}
330+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.imsglobal.lti.launch;
2+
3+
import org.apache.http.HttpRequest;
4+
import org.apache.http.client.methods.HttpGet;
5+
import org.apache.http.client.methods.HttpPost;
6+
import org.apache.http.client.methods.HttpPut;
7+
import org.junit.Test;
8+
9+
import static org.junit.Assert.*;
10+
import java.net.URI;
11+
12+
13+
/**
14+
* Created by paul on 8/24/16.
15+
*/
16+
public class LtiVerifierAndSignerTest {
17+
18+
LtiVerifier verifier = new LtiOauthVerifier();
19+
LtiSigner signer = new LtiOauthSigner();
20+
21+
@Test
22+
public void verifierShouldVerifyCorrectlySignedLtiLaunches() throws Exception {
23+
24+
String key = "key";
25+
String secret = "secret";
26+
HttpPost ltiLaunch = new HttpPost(new URI("http://example.com/test"));
27+
28+
signer.sign(ltiLaunch, key, secret);
29+
LtiVerificationResult result = verifier.verify(new MockHttpPost(ltiLaunch), secret);
30+
31+
assertTrue(result.getSuccess());
32+
}
33+
34+
@Test
35+
public void verifierShouldRejectIncorrectlySignedLtiLaunches() throws Exception {
36+
37+
String key = "key";
38+
String secret = "secret";
39+
HttpPost ltiLaunch = new HttpPost(new URI("http://example.com/test"));
40+
41+
signer.sign(ltiLaunch, key, secret);
42+
LtiVerificationResult result = verifier.verify(new MockHttpPost(ltiLaunch), "wrongSecret");
43+
44+
assertFalse(result.getSuccess());
45+
}
46+
47+
}

0 commit comments

Comments
 (0)