package
0.8.1
Repository: https://github.com/goplus/yap.git
Documentation: pkg.go.dev

# README

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

# Packages

No description provided by the author
No description provided by the author

# Functions

Bearer creates a Bearer Authorization by specified token.
Form encodes a map value into string in form format.
Gopt_App_Main is required by Go+ compiler as the Main entry of a YAP testing project.
Gopt_App_TestMain is required by Go+ compiler as the TestMain entry of a YAP testing project.
Gopt_CaseApp_TestMain is required by Go+ compiler as the entry of a YAP test case.
JsonEncode encodes a value into string in json format.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Interfaces

No description provided by the author

# Type aliases

No description provided by the author