dreamhead / moco

Easy Setup Stub Server
MIT License
4.35k stars 1.08k forks source link

added rule match #303

Closed tian-pengfei closed 1 year ago

tian-pengfei commented 2 years ago

eg:

  @Test
  public void should_match_rule_json() throws Exception {
        final String ruleJsonText = Jsons.toJson("#value['foo'] == 'bar2'");
//        final String ruleJsonText = Jsons.toJson(of("foo", "#value == 'bar2'"));
        final String requestJsonText = Jsons.toJson(of("foo", "bar2"));
        server.request(rule(json(ruleJsonText))).response("foo");
        running(server, () -> assertThat(helper.postContent(root(), requestJsonText), is("foo")));
    }

result:

Results: SUCCESS (1 tests, 1 successes, 0 failures, 0 skipped)
BUILD SUCCESSFUL in 4s

eg:

  @Test
    public void should_match_rule_json_with_resource() throws Exception {

        final String ruleJsonText = "\"#value[name].equalsIgnoreCase('linus')||#value[name]=='Dreamhead'\"";
//        final String ruleJsonText = "{\"name\":\"#value.equalsIgnoreCase('linus')\"}";
        final String requestJsonText = "{\"name\":\"Linus\",\"location\":\"Portland\"}";
        final String responseText = "${req.json.name} is such a cool guy from ${req.json.location}.";
        server.request(rule(json(ruleJsonText))).response(template(responseText));
        running(server, () -> assertThat(helper.postContent(root(), requestJsonText), is("Linus is such a cool guy from Portland.")));
    }

result:

Results: SUCCESS (1 tests, 1 successes, 0 failures, 0 skipped)
BUILD SUCCESSFUL in 5s
tian-pengfei commented 2 years 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
tian-pengfei commented 2 years ago

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
tian-pengfei commented 2 years ago

@dreamhead If you have any suggestions, please let me know in time.