Tests: Go application tests.

This commit is contained in:
Andrey Zelenkov
2018-06-08 18:32:55 +03:00
parent 2b39ed5dfb
commit f2a856aa3b
11 changed files with 377 additions and 5 deletions

6
test/go/404/404.html Normal file
View File

@@ -0,0 +1,6 @@
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
</body>
</html>

22
test/go/404/app.go Normal file
View File

@@ -0,0 +1,22 @@
package main
import (
"io"
"io/ioutil"
"net/http"
"nginx/unit"
)
func handler(w http.ResponseWriter, r *http.Request) {
b, e := ioutil.ReadFile("404.html")
if e == nil {
w.WriteHeader(http.StatusNotFound)
io.WriteString(w, string(b))
}
}
func main() {
http.HandleFunc("/", handler)
unit.ListenAndServe(":7080", nil)
}

View File

@@ -0,0 +1,23 @@
package main
import (
"io"
"os"
"fmt"
"strings"
"net/http"
"nginx/unit"
)
func handler(w http.ResponseWriter, r *http.Request) {
args := strings.Join(os.Args[1:], ",")
w.Header().Add("X-Arg-0", fmt.Sprintf("%v", os.Args[0]))
w.Header().Add("Content-Length", fmt.Sprintf("%v", len(args)))
io.WriteString(w, args)
}
func main() {
http.HandleFunc("/", handler)
unit.ListenAndServe(":7080", nil)
}

19
test/go/cookies/app.go Normal file
View File

@@ -0,0 +1,19 @@
package main
import (
"net/http"
"nginx/unit"
)
func handler(w http.ResponseWriter, r *http.Request) {
cookie1, _ := r.Cookie("var1")
cookie2, _ := r.Cookie("var2")
w.Header().Set("X-Cookie-1", cookie1.Value)
w.Header().Set("X-Cookie-2", cookie2.Value)
}
func main() {
http.HandleFunc("/", handler)
unit.ListenAndServe(":7080", nil)
}

13
test/go/empty/app.go Normal file
View File

@@ -0,0 +1,13 @@
package main
import (
"net/http"
"nginx/unit"
)
func handler(w http.ResponseWriter, r *http.Request) {}
func main() {
http.HandleFunc("/", handler)
unit.ListenAndServe(":7080", nil)
}

View File

@@ -0,0 +1,17 @@
package main
import (
"net/http"
"nginx/unit"
)
func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Var-1", r.URL.Query().Get("var1"))
w.Header().Set("X-Var-2", r.URL.Query().Get("var2"))
w.Header().Set("X-Var-3", r.URL.Query().Get("var3"))
}
func main() {
http.HandleFunc("/", handler)
unit.ListenAndServe(":7080", nil)
}

21
test/go/mirror/app.go Normal file
View File

@@ -0,0 +1,21 @@
package main
import (
"io"
"fmt"
"net/http"
"nginx/unit"
)
func handler(w http.ResponseWriter, r *http.Request) {
var buf [32768]byte;
len, _ := r.Body.Read(buf[:])
w.Header().Add("Content-Length", fmt.Sprintf("%v", len))
io.WriteString(w, string(buf[:len]))
}
func main() {
http.HandleFunc("/", handler)
unit.ListenAndServe(":7080", nil)
}

View File

@@ -0,0 +1,19 @@
package main
import (
"net/http"
"nginx/unit"
)
func handler(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
w.Header().Set("X-Var-1", r.Form.Get("var1"))
w.Header().Set("X-Var-2", r.Form.Get("var2"))
w.Header().Set("X-Var-3", r.Form.Get("var3"))
}
func main() {
http.HandleFunc("/", handler)
unit.ListenAndServe(":7080", nil)
}

30
test/go/variables/app.go Normal file
View File

@@ -0,0 +1,30 @@
package main
import (
"io"
"fmt"
"net/http"
"nginx/unit"
)
func handler(w http.ResponseWriter, r *http.Request) {
var buf [4096]byte;
len, _ := r.Body.Read(buf[:])
w.Header().Set("Request-Method", r.Method)
w.Header().Set("Request-Uri", r.RequestURI)
w.Header().Set("Server-Protocol", r.Proto)
w.Header().Set("Server-Protocol-Major", fmt.Sprintf("%v", r.ProtoMajor))
w.Header().Set("Server-Protocol-Minor", fmt.Sprintf("%v", r.ProtoMinor))
w.Header().Set("Content-Length", fmt.Sprintf("%v", len))
w.Header().Set("Content-Type", r.Header.Get("Content-Type"))
w.Header().Set("Custom-Header", r.Header.Get("Custom-Header"))
w.Header().Set("Http-Host", r.Header.Get("Host"))
io.WriteString(w, string(buf[:len]))
}
func main() {
http.HandleFunc("/", handler)
unit.ListenAndServe(":7080", nil)
}