Skip to content

Latest commit

 

History

History

ytest

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

yaptest - Go+ HTTP Test Framework

Language GitHub release Discord GoDoc

yaptest is a web server testing framework. This classfile has the file suffix _ytest.gox.

Before using yaptest, you need to add github.com/goplus/yap to go.mod:

gop get github.com/goplus/yap@latest

Suppose we have a web server (foo/get_p_#id.yap):

json {
	"id": ${id},
}

Then we create a yaptest file (foo/foo_ytest.gox):

mock "foo.com", new(AppV2)  // name of any YAP v2 web server is `AppV2`

id := "123"
get "http://foo.com/p/${id}"
ret 200
json {
	"id": id,
}

The directive mock creates the web server by mockhttp. Then we write test code directly.

You can change the directive mock to testServer (see foo/bar_ytest.gox), and keep everything else unchanged:

testServer "foo.com", new(AppV2)

id := "123"
get "http://foo.com/p/${id}"
ret 200
json {
	"id": id,
}

The directive testServer creates the web server by net/http/httptest and obtained a random port as the service address. Then it calls the directive host to map the random service address to foo.com. This makes all other code no need to changed.

match

This is almost the core concept in yaptest. It matches two objects.

Let’s look at a simple example first:

id := Var(int)
match id, 1+2
echo id

Here we define a variable called id and match it with expression 1+2. If the variable is unbound, it is assigned the value of the expression. In this way the value of id becomes 3.

So far, you've seen match like the assignment side. But you cannot assign a different value to a variable that has been bound:

id := Var(int)
match id, 1+2
match id, 3
echo id

match id, 5  // unmatched value - expected: 3, got: 5

In the second match statement, the variable id has been bound. At this time, it will be compared with the expression value. If it is equal, it will succeed, otherwise an error will be reported (such as the third match statement above).

The match statement can be complex, such as:

d := Var(string)

match {
    "c": {"d": d},
}, {
    "a": 1,
    "b": 3.14,
    "c": {"d": "hello", "e": "world"},
    "f": 1,
}

echo d
match d, "hello"

Generally, the syntax of the match command is:

match <ExpectedObject> <SourceObject>

Unbound variables are allowed in <ExpectedObject>, but cannot appear in <SourceObject>. <ExpectedObject> and <SourceObject> do not have to be exactly the same, but what appears in <ExpectedObject> must also appear in <SourceObject>. That is, it is required to be a subset relationship (<ExpectedObject> is a subset of <SourceObject>).

If a variable in <ExpectedObject> has not been bound, it will be bound according to the value of the corresponding <SourceObject>; if the variable has been bound, the values on both sides must match.

The cornerstone of yaptest is matching grammar. Let's look at the example you saw at the beginning:

id := "123"
get "http://foo.com/p/${id}"

ret 200
json {
	"id": id,
}

It is equivalent to:

id := "123"
get "http://foo.com/p/${id}"

send                 // send request
match 200, resp.code // assert resp.code == 200
match "application/json", resp.header.get("Content-Type")
match {              // assert resp.body.id == id
	"id": id,
}, resp.body

host

host <host> <real>

The Host directive allows you to use an abstract domain name (which does not necessarily exist) to represent a service.

For examples:

host "https://foo.com", "http://127.0.0.1:8080"

The host directive makes it easy for you to use the same test code between your test environment and production environment:

host "https://foo.com", ${FOO_HOST}
// Let env FOO_HOST point to the test environment or production environment

req/post/get/delete/put

TODO

header

TODO

ret

TODO

body/json/form/text/binary

TODO

auth

auth <auth>

Auth sets an Authorization for a request. For example:

auth qiniu("<AccessKey>", "<SecretKey>")

Authorization information can be shared between different requests:

testauth := qiniu("<AccessKey>", "<SecretKey>")

post "https://foo.com/bar"
auth testauth
ret 200

...

get "https://foo.com/bar"
auth testauth
ret 200