Skip to content

Commit 8d68063

Browse files
authored
Merge pull request zaproxy#9012 from psiinon/sstruct-tests
SessionStructure - remove deprecated methods and add tests
2 parents d314b5e + d8c3934 commit 8d68063

3 files changed

Lines changed: 133 additions & 93 deletions

File tree

zap/gradle/japicmp.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@ fieldExcludes: []
77
classExcludes:
88
- "org.parosproxy.paros.core.scanner.VariantAbstractRPCQuery$RPCParameter"
99
- "org.parosproxy.paros.core.scanner.VariantJSONQuery$SimpleStringReader"
10-
methodExcludes: []
10+
methodExcludes:
11+
- "org.zaproxy.zap.model.SessionStructure#addPath(org.parosproxy.paros.model.Session,org.parosproxy.paros.model.HistoryReference,org.parosproxy.paros.network.HttpMessage)"
12+
- "org.zaproxy.zap.model.SessionStructure#addPath(org.parosproxy.paros.model.Session,org.parosproxy.paros.model.HistoryReference,org.parosproxy.paros.network.HttpMessage,boolean)"
13+
- "org.zaproxy.zap.model.SessionStructure#find(long,org.apache.commons.httpclient.URI,java.lang.String,java.lang.String)"
14+
- "org.zaproxy.zap.model.SessionStructure#getNodeName(org.parosproxy.paros.network.HttpMessage)"
15+
- "org.zaproxy.zap.model.SessionStructure#getRootNode()"

zap/src/main/java/org/zaproxy/zap/model/SessionStructure.java

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,6 @@ public class SessionStructure {
5353

5454
private static final Logger LOGGER = LogManager.getLogger(SessionStructure.class);
5555

56-
/**
57-
* Adds the message to the Sites tree
58-
*
59-
* @param session the session
60-
* @param ref the history reference
61-
* @param msg the message
62-
* @return the node added to the Sites Tree
63-
* @deprecated Use {@link #addPath(Model, HistoryReference, HttpMessage)}
64-
*/
65-
@Deprecated
66-
public static StructuralNode addPath(Session session, HistoryReference ref, HttpMessage msg) {
67-
return addPath(session, ref, msg, false);
68-
}
69-
7056
/**
7157
* Adds the message to the Sites tree
7258
*
@@ -80,23 +66,6 @@ public static StructuralNode addPath(Model model, HistoryReference ref, HttpMess
8066
return addPath(model, ref, msg, false);
8167
}
8268

83-
/**
84-
* Adds the message to the Sites tree
85-
*
86-
* @param session the session
87-
* @param ref the history reference
88-
* @param msg the message
89-
* @param newOnly Only return a SiteNode if one was newly created
90-
* @return the SiteNode that corresponds to the HttpMessage, or null if newOnly and the node
91-
* already exists
92-
* @deprecated Use {@link #addPath(Model, HistoryReference, HttpMessage, boolean)}
93-
*/
94-
@Deprecated
95-
public static StructuralNode addPath(
96-
Session session, HistoryReference ref, HttpMessage msg, boolean newOnly) {
97-
return addPath(Model.getSingleton(), ref, msg, newOnly);
98-
}
99-
10069
/**
10170
* Adds the message to the Sites tree
10271
*
@@ -189,38 +158,6 @@ public static StructuralNode find(Model model, HttpMessage msg)
189158
return new StructuralTableNode(rs);
190159
}
191160

192-
/**
193-
* Finds the node in the Site tree for the given request data
194-
*
195-
* @param sessionId the session id
196-
* @param uri the URI
197-
* @param method the method
198-
* @param postData the POST data
199-
* @return the site node or null if not found
200-
* @throws DatabaseException
201-
* @throws URIException
202-
* @deprecated Use {@link #find(Model, URI, String, String)}
203-
*/
204-
@Deprecated
205-
public static StructuralNode find(long sessionId, URI uri, String method, String postData)
206-
throws DatabaseException, URIException {
207-
Model model = Model.getSingleton();
208-
if (!Constant.isLowMemoryOptionSet()) {
209-
SiteNode node = model.getSession().getSiteTree().findNode(uri, method, postData);
210-
if (node == null) {
211-
return null;
212-
}
213-
return new StructuralSiteNode(node);
214-
}
215-
216-
String nodeName = getNodeName(model, uri, method, postData, null);
217-
RecordStructure rs = model.getDb().getTableStructure().find(sessionId, nodeName, method);
218-
if (rs == null) {
219-
return null;
220-
}
221-
return new StructuralTableNode(rs);
222-
}
223-
224161
/**
225162
* Finds the node in the Site tree for the given request data
226163
*
@@ -284,19 +221,6 @@ private static String getNodeName(
284221
return nodeUrl;
285222
}
286223

287-
/**
288-
* Returns the node name for the given message
289-
*
290-
* @param msg the message
291-
* @return the node name
292-
* @throws URIException
293-
* @deprecated Use {@link #getNodeName(Model, HttpMessage)}
294-
*/
295-
@Deprecated
296-
public static String getNodeName(HttpMessage msg) throws URIException {
297-
return getNodeName(Model.getSingleton(), msg);
298-
}
299-
300224
/**
301225
* Returns the node name for the given message
302226
*
@@ -697,17 +621,6 @@ private static String getScheme(URI uri) {
697621
return scheme.toLowerCase(Locale.ROOT);
698622
}
699623

700-
/**
701-
* Returns the root node
702-
*
703-
* @return the root node
704-
* @deprecated Use {@link #getRootNode(Model)}
705-
*/
706-
@Deprecated
707-
public static StructuralNode getRootNode() {
708-
return getRootNode(Model.getSingleton());
709-
}
710-
711624
/**
712625
* Returns the root node
713626
*

zap/src/test/java/org/zaproxy/zap/model/SessionStructureUnitTest.java

Lines changed: 127 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -472,14 +472,136 @@ void shouldReturnOverridenPathTree() throws Exception {
472472
}
473473
}
474474

475+
@Nested
476+
static class NodeNameTests {
477+
478+
private Model model;
479+
private Session session;
480+
private VariantFactory factory;
481+
482+
HttpMessage getParams;
483+
HttpMessage postParamsFormData;
484+
HttpMessage postParamsJsonData;
485+
HttpMessage postParamsXmlData;
486+
HttpMessage postMultipartData;
487+
488+
@BeforeEach
489+
void setup() throws Exception {
490+
WithConfigsTest.setUpConstantMessages();
491+
model = mock(Model.class);
492+
session = new Session(model);
493+
factory = new VariantFactory();
494+
given(model.getSession()).willReturn(session);
495+
given(model.getVariantFactory()).willReturn(factory);
496+
getParams =
497+
new HttpMessage(new URI("https://www.example.com/aaa/bbb?aa=bb&cc=dd", false));
498+
postParamsFormData =
499+
getPostMsgWithFormParams(
500+
"https://www.example.com/ccc", "aa=bb&cc=dd", "ee=ff&gg=ee");
501+
postParamsJsonData =
502+
getPostMsg(
503+
"https://www.example.com/ccc",
504+
"aa=bb&cc=dd",
505+
"{\"aaa\":\"bbb\", \"ccc\":\"ddd\", \"eee\":\"fff\"}",
506+
"application/json");
507+
postParamsXmlData =
508+
getPostMsg(
509+
"https://www.example.com/ccc",
510+
"aa=bb&cc=dd",
511+
"<aaa><bbb>BBB</bbb><ccc>CCC</ccc><ddd>DDD</ddd></aaa>",
512+
"text/xml");
513+
Control.initSingletonForTesting(model);
514+
}
515+
516+
@AfterEach
517+
void cleanUp() {
518+
Constant.messages = null;
519+
}
520+
521+
@Test
522+
void shouldGetNodeName() throws URIException {
523+
assertThat(
524+
SessionStructure.getNodeName(model, getParams),
525+
is(equalTo("https://www.example.com/aaa/bbb (aa,cc)")));
526+
assertThat(
527+
SessionStructure.getNodeName(model, postParamsFormData),
528+
is(equalTo("https://www.example.com/ccc (aa,cc)(ee,gg)")));
529+
// FIXME should have the JSON key names
530+
assertThat(
531+
SessionStructure.getNodeName(model, postParamsJsonData),
532+
is(equalTo("https://www.example.com/ccc (aa,cc)")));
533+
// FIXME should have the XML key names
534+
assertThat(
535+
SessionStructure.getNodeName(model, postParamsXmlData),
536+
is(equalTo("https://www.example.com/ccc (aa,cc)")));
537+
}
538+
539+
@Test
540+
void shouldGetLeafName1() throws URIException {
541+
assertThat(
542+
SessionStructure.getLeafName(model, "test", getParams),
543+
is(equalTo("GET:test(aa,cc)")));
544+
assertThat(
545+
SessionStructure.getLeafName(model, "test", postParamsFormData),
546+
is(equalTo("POST:test(aa,cc)(ee,gg)")));
547+
// FIXME should have the JSON key names
548+
assertThat(
549+
SessionStructure.getLeafName(model, "test", postParamsJsonData),
550+
is(
551+
equalTo(
552+
"POST:test(aa,cc)({\"aaa\":\"bbb\", \"ccc\":\"ddd\", \"eee\":\"fff\"})")));
553+
// FIXME should have the XML key names
554+
assertThat(
555+
SessionStructure.getLeafName(model, "test", postParamsXmlData),
556+
is(equalTo("POST:test(aa,cc)(<aaa><bbb>BBB</bbb><ccc>CCC</ccc><ddd>DD...)")));
557+
}
558+
559+
@Test
560+
void shouldGetLeafName2() throws Exception {
561+
assertThat(getLeafName2(getParams), is(equalTo("GET:test(aa,cc)")));
562+
assertThat(getLeafName2(postParamsFormData), is(equalTo("POST:test(aa,cc)(ee,gg)")));
563+
// FIXME should have the JSON key names
564+
assertThat(
565+
getLeafName2(postParamsJsonData),
566+
is(
567+
equalTo(
568+
"POST:test(aa,cc)({\"aaa\":\"bbb\", \"ccc\":\"ddd\", \"eee\":\"fff\"})")));
569+
// FIXME should have the XML key names
570+
assertThat(
571+
getLeafName2(postParamsXmlData),
572+
is(equalTo("POST:test(aa,cc)(<aaa><bbb>BBB</bbb><ccc>CCC</ccc><ddd>DD...)")));
573+
}
574+
575+
String getLeafName2(HttpMessage msg) throws Exception {
576+
return SessionStructure.getLeafName(
577+
model,
578+
"test",
579+
msg.getRequestHeader().getURI(),
580+
msg.getRequestHeader().getMethod(),
581+
msg.getRequestBody().toString());
582+
}
583+
}
584+
475585
private void createPostMsgWithFormParams(String uri, String queryParams, String formParams)
476586
throws URIException {
477-
msg.getRequestHeader().setMethod(HttpRequestHeader.POST);
587+
msg = getPostMsgWithFormParams(uri, queryParams, formParams);
588+
}
589+
590+
private static HttpMessage getPostMsgWithFormParams(
591+
String uri, String queryParams, String formParams) throws URIException {
592+
return getPostMsg(uri, queryParams, formParams, "application/x-www-form-urlencoded");
593+
}
594+
595+
private static HttpMessage getPostMsg(
596+
String uri, String queryParams, String formParams, String contentType)
597+
throws URIException {
598+
HttpMessage message = new HttpMessage();
599+
message.getRequestHeader().setMethod(HttpRequestHeader.POST);
478600
queryParams = queryParams == null ? "" : "?" + queryParams;
479-
msg.getRequestHeader().setURI(new URI(uri + queryParams, true));
480-
msg.getRequestHeader()
481-
.setHeader(HttpHeader.CONTENT_TYPE, "application/x-www-form-urlencoded");
482-
msg.setRequestBody(formParams);
601+
message.getRequestHeader().setURI(new URI(uri + queryParams, true));
602+
message.getRequestHeader().setHeader(HttpHeader.CONTENT_TYPE, contentType);
603+
message.setRequestBody(formParams);
604+
return message;
483605
}
484606

485607
public static final class PathTreeVariant implements Variant {

0 commit comments

Comments
 (0)