Closed tian-pengfei closed 1 year ago
rule match in java(commit- d37939a):
@Test
public void should_rule() throws Exception {
server.request(rule(uri("#value.startsWith('/foo')"))).response("bar");
running(server, () -> {
assertThat(helper.get(remoteUrl("/foo/a")), is("bar"));
assertThat(helper.get(remoteUrl("/foo/b")), is("bar"));
});
}
result:
Results: SUCCESS (1 tests, 1 successes, 0 failures, 0 skipped)
BUILD SUCCESSFUL in 11s
4 actionable tasks: 3 executed, 1 up-to-date
@Test
public void should_rule_for_resource() throws Exception {
server.request(rule(header("foo"), "#value.endsWith('bar')")).response("bar");
running(server, () -> {
assertThat(helper.getWithHeader(root(), of("foo", "Abar")), is("bar"));
assertThat(helper.getWithHeader(root(), of("foo", "Bbar")), is("bar"));
});
}
result:
Results: SUCCESS (1 tests, 1 successes, 0 failures, 0 skipped)
BUILD SUCCESSFUL in 4s
4 actionable tasks: 1 executed, 3 up-to-date
rule match in json(commit- 569df57): java:
@Test
public void should_match_uri() throws IOException {
runWithConfiguration("rule.json");
assertThat(helper.get(remoteUrl("/foo/bar")), is("uri_match"));
assertThat(helper.get(remoteUrl("/foo/blah")), is("uri_match"));
}
@Test
public void should_match_text() throws IOException {
runWithConfiguration("rule.json");
assertThat(helper.postContent(remoteUrl("/text-match"), "foobar"), is("text_match"));
assertThat(helper.postContent(remoteUrl("/text-match"), "fooblah"), is("text_match"));
}
@Test
public void should_match_header() throws IOException {
runWithConfiguration("rule.json");
assertThat(helper.getWithHeader(remoteUrl("/header-match"), of(HttpHeaders.CONTENT_TYPE, "application/json")), is("header_match"));
assertThat(helper.getWithHeader(remoteUrl("/header-match"), of(HttpHeaders.CONTENT_TYPE, "application/xml")), is("header_match"));
}
@Test
public void should_match_query() throws IOException {
runWithConfiguration("rule.json");
assertThat(helper.get(remoteUrl("/query-match?foo=bar")), is("query_match"));
assertThat(helper.get(remoteUrl("/query-match?foo=blah")), is("query_match"));
}
@Test
public void should_match_json() throws IOException {
runWithConfiguration("rule.json");
assertThat(helper.postContent(remoteUrl("/json-match"), "{\n\t\"name\":\"Linus\"\n}"), is("json_match"));
assertThat(helper.postContent(remoteUrl("/json-match"), "{\n\t\"name\":\"linus\"\n}"), is("json_match"));
}
the content of file:
[
{
"request": {
"uri": {
"rule": "#value.startsWith('/foo')"
}
},
"response": {
"text": "uri_match"
}
},
{
"request": {
"uri": "/text-match",
"text": {
"rule": "#value.startsWith('foo')"
}
},
"response": {
"text": "text_match"
}
},
{
"request": {
"uri": "/header-match",
"headers": {
"content-type": {
"rule": "#value.startsWith('application')"
}
}
},
"response": {
"text": "header_match"
}
},
{
"request": {
"uri": "/query-match",
"queries": {
"foo": {
"rule": "#value.startsWith('b')"
}
}
},
"response": {
"text": "query_match"
}
},
{
"request": {
"uri": "/json-match",
"json_rule": {
"name": "#value.equalsIgnoreCase('Linus')"
}
},
"response": {
"text": "json_match"
}
}
]
The result of executing Mocoruletest.java :
GET /query-match HTTP/1.1
content-length: 0
Connection: Keep-Alive
Host: localhost:12306
Accept-Encoding: gzip,deflate
24 九月 2021 11:37:04 [pool-1-thread-2] INFO Response return:
HTTP/1.1 200
Content-Length: 11
Content-Type: text/plain; charset=utf-8
query_match
24 九月 2021 11:37:04 [pool-1-thread-2] INFO Request received:
GET /query-match HTTP/1.1
content-length: 0
Connection: Keep-Alive
Host: localhost:12306
Accept-Encoding: gzip,deflate
24 九月 2021 11:37:04 [pool-1-thread-2] INFO Response return:
HTTP/1.1 200
Content-Length: 11
Content-Type: text/plain; charset=utf-8
query_match
24 九月 2021 11:37:04 [Test worker] INFO Server stopped.
24 九月 2021 11:37:04 [Test worker] INFO Server is started at 12306
24 九月 2021 11:37:04 [pool-1-thread-14] INFO Request received:
GET /foo/bar HTTP/1.1
content-length: 0
Connection: Keep-Alive
Host: localhost:12306
Accept-Encoding: gzip,deflate
24 九月 2021 11:37:04 [pool-1-thread-14] INFO Response return:
HTTP/1.1 200
Content-Length: 9
Content-Type: text/plain; charset=utf-8
uri_match
24 九月 2021 11:37:04 [pool-1-thread-14] INFO Request received:
GET /foo/blah HTTP/1.1
content-length: 0
Connection: Keep-Alive
Host: localhost:12306
Accept-Encoding: gzip,deflate
24 九月 2021 11:37:04 [pool-1-thread-14] INFO Response return:
HTTP/1.1 200
Content-Length: 9
Content-Type: text/plain; charset=utf-8
uri_match
24 九月 2021 11:37:04 [Test worker] INFO Server stopped.
24 九月 2021 11:37:04 [Test worker] INFO Server is started at 12306
24 九月 2021 11:37:04 [pool-1-thread-10] INFO Request received:
GET /header-match HTTP/1.1
content-length: 0
Connection: Keep-Alive
Host: localhost:12306
Accept-Encoding: gzip,deflate
Content-Type: application/json
24 九月 2021 11:37:04 [pool-1-thread-10] INFO Response return:
HTTP/1.1 200
Content-Length: 12
Content-Type: text/plain; charset=utf-8
header_match
24 九月 2021 11:37:04 [pool-1-thread-10] INFO Request received:
GET /header-match HTTP/1.1
content-length: 0
Connection: Keep-Alive
Host: localhost:12306
Accept-Encoding: gzip,deflate
Content-Type: application/xml
24 九月 2021 11:37:04 [pool-1-thread-10] INFO Response return:
HTTP/1.1 200
Content-Length: 12
Content-Type: text/plain; charset=utf-8
header_match
24 九月 2021 11:37:04 [Test worker] INFO Server stopped.
24 九月 2021 11:37:04 [Test worker] INFO Server is started at 12306
24 九月 2021 11:37:04 [pool-1-thread-13] INFO Request received:
POST /json-match HTTP/1.1
content-length: 19
Connection: Keep-Alive
Host: localhost:12306
Accept-Encoding: gzip,deflate
Content-Type: text/plain; charset=UTF-8
{
"name":"Linus"
}
24 九月 2021 11:37:04 [pool-1-thread-13] INFO Response return:
HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8
json_match
24 九月 2021 11:37:04 [pool-1-thread-13] INFO Request received:
POST /json-match HTTP/1.1
content-length: 19
Connection: Keep-Alive
Host: localhost:12306
Accept-Encoding: gzip,deflate
Content-Type: text/plain; charset=UTF-8
{
"name":"linus"
}
24 九月 2021 11:37:04 [pool-1-thread-13] INFO Response return:
HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8
json_match
24 九月 2021 11:37:04 [Test worker] INFO Server stopped.
24 九月 2021 11:37:04 [Test worker] INFO Server is started at 12306
24 九月 2021 11:37:04 [pool-1-thread-3] INFO Request received:
POST /text-match HTTP/1.1
content-length: 6
Connection: Keep-Alive
Host: localhost:12306
Accept-Encoding: gzip,deflate
Content-Type: text/plain; charset=UTF-8
foobar
24 九月 2021 11:37:04 [pool-1-thread-3] INFO Response return:
HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8
text_match
24 九月 2021 11:37:04 [pool-1-thread-3] INFO Request received:
POST /text-match HTTP/1.1
content-length: 7
Connection: Keep-Alive
Host: localhost:12306
Accept-Encoding: gzip,deflate
Content-Type: text/plain; charset=UTF-8
fooblah
24 九月 2021 11:37:04 [pool-1-thread-3] INFO Response return:
HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8
text_match
24 九月 2021 11:37:04 [Test worker] INFO Server stopped.
Disconnected from the target VM, address: 'localhost:1726', transport: 'socket'
Results: SUCCESS (5 tests, 5 successes, 0 failures, 0 skipped)
BUILD SUCCESSFUL in 5s
@dreamhead If you have any suggestions, please let me know in time.
eg:
result:
eg:
result: