mirror of
https://github.com/SourceFellows/gobuch.git
synced 2025-09-19 02:24:54 +02:00
initial import
This commit is contained in:
3
quality/http-server-test/go.mod
Normal file
3
quality/http-server-test/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module golang.source-fellows.com/samples/http-server-test
|
||||
|
||||
go 1.14
|
29
quality/http-server-test/main.go
Normal file
29
quality/http-server-test/main.go
Normal file
@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var jsonDoc1 = []byte("{\"Name\":\"")
|
||||
var jsonDoc2 = []byte("\"}")
|
||||
|
||||
func myHandler(writer http.ResponseWriter, req *http.Request) {
|
||||
path := req.URL.Path
|
||||
|
||||
if len(path) < 6 || path[:6] != "/json/" {
|
||||
writer.WriteHeader(http.StatusNotImplemented)
|
||||
return
|
||||
}
|
||||
|
||||
writer.Header().Add("Content-Type", "application/json")
|
||||
bites := make([]byte, 20)
|
||||
bites = append(bites, jsonDoc1...)
|
||||
bites = append(bites, path[6:]...)
|
||||
bites = append(bites, jsonDoc2...)
|
||||
writer.Write(bites)
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", myHandler)
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
40
quality/http-server-test/main_test.go
Normal file
40
quality/http-server-test/main_test.go
Normal file
@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_myHandler(t *testing.T) {
|
||||
type args struct {
|
||||
url string
|
||||
expectedName string
|
||||
status int
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
}{
|
||||
{"t1", args{"/json/test", "test", http.StatusOK}},
|
||||
{"t2", args{"/x/y", "", http.StatusNotImplemented}},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
url := tt.args.url
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
t.Errorf("could't create request: %v", tt.args.url)
|
||||
}
|
||||
recorder := httptest.NewRecorder()
|
||||
myHandler(recorder, req)
|
||||
if recorder.Result().StatusCode != tt.args.status {
|
||||
t.Errorf("Wrong status code %v, expected %v", recorder.Result().StatusCode, tt.args.status)
|
||||
}
|
||||
if !strings.Contains(recorder.Body.String(), tt.args.expectedName) {
|
||||
t.Errorf("The response does't contain '%v': '%v'", tt.args.expectedName, recorder.Body.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user