initial import

This commit is contained in:
SourceFellows
2020-08-21 06:26:40 +02:00
commit e223458dd4
423 changed files with 9871 additions and 0 deletions

View File

@ -0,0 +1,3 @@
module golang.source-fellows.com/samples/http-server-test
go 1.14

View File

@ -0,0 +1,50 @@
/*Package main zeigt einen einfachen HTTP Service, der getestet werden soll.
Das Abschalten des Power-Safe Modus unter Linux hilft folgendes Script:
for i in /sys/devices/system/cpu/cpu[0-7]
do
echo performance > $i/cpufreq/scaling_governor
done
Die Laufzeitgeschwindigkeit kann zusätzlich über 'ab' (Apache HTTP server benchmarking tool -
https://httpd.apache.org/docs/2.4/programs/ab.html) betestet werden.
*/
package main
import (
"encoding/json"
"net/http"
_ "net/http/pprof"
"regexp"
)
type Result struct {
Name string
}
func myHandler(writer http.ResponseWriter, req *http.Request) {
path := req.URL.Path
re := regexp.MustCompile("^/json/(.*)$")
if !re.MatchString(path) {
writer.WriteHeader(401)
return
}
parts := re.FindStringSubmatch(path)
res := &Result{Name: parts[1]}
bites, err := json.Marshal(*res)
if err != nil {
writer.WriteHeader(500)
return
}
writer.Header().Add("Content-Type", "application/json")
writer.Write(bites)
}
func main() {
http.HandleFunc("/", myHandler)
http.ListenAndServe(":8080", nil)
}

View File

@ -0,0 +1,36 @@
package main
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func Test_myHandler(t *testing.T) {
type args struct {
url string
expectedName string
}
tests := []struct {
name string
args args
}{
{"simple URL", args{"/json/testing", "testing"}},
{"simple URL", args{"/json/test/2", "test"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
request, err := http.NewRequest(http.MethodGet, tt.args.url, nil)
if err != nil {
t.Errorf("could not create request for url: %v", tt.args.url)
}
recorder := httptest.NewRecorder()
myHandler(recorder, request)
if !strings.Contains(recorder.Body.String(), tt.args.expectedName) {
t.Errorf("The response does't contain %v", tt.args.expectedName)
}
})
}
}

View File

@ -0,0 +1,3 @@
module golang.source-fellows.com/samples/http-server-test
go 1.14

View File

@ -0,0 +1,37 @@
package main
import (
"encoding/json"
"net/http"
_ "net/http/pprof"
"regexp"
)
var re = regexp.MustCompile("^/json/(.*)$")
type Result struct {
Name string
}
func myHandler(writer http.ResponseWriter, req *http.Request) {
path := req.URL.Path
if !re.MatchString(path) {
writer.WriteHeader(http.StatusNotImplemented)
return
}
parts := re.FindStringSubmatch(path)
res := &Result{Name: parts[1]}
bites, err := json.Marshal(*res)
if err != nil {
writer.WriteHeader(500)
return
}
writer.Header().Add("Content-Type", "application/json")
writer.Write(bites)
}
func main() {
http.HandleFunc("/", myHandler)
http.ListenAndServe(":8080", nil)
}

View 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())
}
})
}
}

View File

@ -0,0 +1,119 @@
<html>
<body>
<h1>Statische Rückgabe</h1>
<pre>
package main
import (
"net/http"
_ "net/http/pprof"
)
type Result struct {
Name string
}
var bites = []byte("{}")
func myHandler(writer http.ResponseWriter, req *http.Request) {
writer.Header().Add("Content-Type", "application/json")
writer.Write(bites)
}
func main() {
http.HandleFunc("/", myHandler)
http.ListenAndServe(":8080", nil)
}
</pre>
<table>
<tr>
<th colspan=2 bgcolor=white>Server Software:</th>
<td colspan=2 bgcolor=white></td>
</tr>
<tr>
<th colspan=2 bgcolor=white>Server Hostname:</th>
<td colspan=2 bgcolor=white>localhost</td>
</tr>
<tr>
<th colspan=2 bgcolor=white>Server Port:</th>
<td colspan=2 bgcolor=white>8080</td>
</tr>
<tr>
<th colspan=2 bgcolor=white>Document Path:</th>
<td colspan=2 bgcolor=white>/Kristian</td>
</tr>
<tr>
<th colspan=2 bgcolor=white>Document Length:</th>
<td colspan=2 bgcolor=white>2 bytes</td>
</tr>
<tr>
<th colspan=2 bgcolor=white>Concurrency Level:</th>
<td colspan=2 bgcolor=white>3</td>
</tr>
<tr>
<th colspan=2 bgcolor=white>Time taken for tests:</th>
<td colspan=2 bgcolor=white>0.401 seconds</td>
</tr>
<tr>
<th colspan=2 bgcolor=white>Complete requests:</th>
<td colspan=2 bgcolor=white>10000</td>
</tr>
<tr>
<th colspan=2 bgcolor=white>Failed requests:</th>
<td colspan=2 bgcolor=white>0</td>
</tr>
<tr>
<th colspan=2 bgcolor=white>Total transferred:</th>
<td colspan=2 bgcolor=white>1090000 bytes</td>
</tr>
<tr>
<th colspan=2 bgcolor=white>HTML transferred:</th>
<td colspan=2 bgcolor=white>20000 bytes</td>
</tr>
<tr>
<th colspan=2 bgcolor=white>Requests per second:</th>
<td colspan=2 bgcolor=white>24916.72</td>
</tr>
<tr>
<th colspan=2 bgcolor=white>Transfer rate:</th>
<td colspan=2 bgcolor=white>2652.27 kb/s received</td>
</tr>
<tr>
<th bgcolor=white colspan=4>Connnection Times (ms)</th>
</tr>
<tr>
<th bgcolor=white>&nbsp;</th>
<th bgcolor=white>min</th>
<th bgcolor=white>avg</th>
<th bgcolor=white>max</th>
</tr>
<tr>
<th bgcolor=white>Connect:</th>
<td bgcolor=white> 0</td>
<td bgcolor=white> 0</td>
<td bgcolor=white> 1</td>
</tr>
<tr>
<th bgcolor=white>Processing:</th>
<td bgcolor=white> 0</td>
<td bgcolor=white> 0</td>
<td bgcolor=white> 0</td>
</tr>
<tr>
<th bgcolor=white>Total:</th>
<td bgcolor=white> 0</td>
<td bgcolor=white> 0</td>
<td bgcolor=white> 1</td>
</tr>
</table>
</body>
</html>

View File

@ -0,0 +1,3 @@
module golang.source-fellows.com/samples/http-server-test
go 1.14

View File

@ -0,0 +1,33 @@
package main
import (
"encoding/json"
"net/http"
_ "net/http/pprof"
)
type Result struct {
Name string
}
func myHandler(writer http.ResponseWriter, req *http.Request) {
path := req.URL.Path
if len(path) < 6 || path[:6] != "/json/" {
writer.WriteHeader(http.StatusNotImplemented)
return
}
res := &Result{Name: path[6:]}
bites, err := json.Marshal(*res)
if err != nil {
writer.WriteHeader(500)
return
}
writer.Header().Add("Content-Type", "application/json")
writer.Write(bites)
}
func main() {
http.HandleFunc("/", myHandler)
http.ListenAndServe(":8080", nil)
}

View 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())
}
})
}
}

View File

@ -0,0 +1,119 @@
<html>
<body>
<h1>Statische Rückgabe</h1>
<pre>
package main
import (
"net/http"
_ "net/http/pprof"
)
type Result struct {
Name string
}
var bites = []byte("{}")
func myHandler(writer http.ResponseWriter, req *http.Request) {
writer.Header().Add("Content-Type", "application/json")
writer.Write(bites)
}
func main() {
http.HandleFunc("/", myHandler)
http.ListenAndServe(":8080", nil)
}
</pre>
<table>
<tr>
<th colspan=2 bgcolor=white>Server Software:</th>
<td colspan=2 bgcolor=white></td>
</tr>
<tr>
<th colspan=2 bgcolor=white>Server Hostname:</th>
<td colspan=2 bgcolor=white>localhost</td>
</tr>
<tr>
<th colspan=2 bgcolor=white>Server Port:</th>
<td colspan=2 bgcolor=white>8080</td>
</tr>
<tr>
<th colspan=2 bgcolor=white>Document Path:</th>
<td colspan=2 bgcolor=white>/Kristian</td>
</tr>
<tr>
<th colspan=2 bgcolor=white>Document Length:</th>
<td colspan=2 bgcolor=white>2 bytes</td>
</tr>
<tr>
<th colspan=2 bgcolor=white>Concurrency Level:</th>
<td colspan=2 bgcolor=white>3</td>
</tr>
<tr>
<th colspan=2 bgcolor=white>Time taken for tests:</th>
<td colspan=2 bgcolor=white>0.401 seconds</td>
</tr>
<tr>
<th colspan=2 bgcolor=white>Complete requests:</th>
<td colspan=2 bgcolor=white>10000</td>
</tr>
<tr>
<th colspan=2 bgcolor=white>Failed requests:</th>
<td colspan=2 bgcolor=white>0</td>
</tr>
<tr>
<th colspan=2 bgcolor=white>Total transferred:</th>
<td colspan=2 bgcolor=white>1090000 bytes</td>
</tr>
<tr>
<th colspan=2 bgcolor=white>HTML transferred:</th>
<td colspan=2 bgcolor=white>20000 bytes</td>
</tr>
<tr>
<th colspan=2 bgcolor=white>Requests per second:</th>
<td colspan=2 bgcolor=white>24916.72</td>
</tr>
<tr>
<th colspan=2 bgcolor=white>Transfer rate:</th>
<td colspan=2 bgcolor=white>2652.27 kb/s received</td>
</tr>
<tr>
<th bgcolor=white colspan=4>Connnection Times (ms)</th>
</tr>
<tr>
<th bgcolor=white>&nbsp;</th>
<th bgcolor=white>min</th>
<th bgcolor=white>avg</th>
<th bgcolor=white>max</th>
</tr>
<tr>
<th bgcolor=white>Connect:</th>
<td bgcolor=white> 0</td>
<td bgcolor=white> 0</td>
<td bgcolor=white> 1</td>
</tr>
<tr>
<th bgcolor=white>Processing:</th>
<td bgcolor=white> 0</td>
<td bgcolor=white> 0</td>
<td bgcolor=white> 0</td>
</tr>
<tr>
<th bgcolor=white>Total:</th>
<td bgcolor=white> 0</td>
<td bgcolor=white> 0</td>
<td bgcolor=white> 1</td>
</tr>
</table>
</body>
</html>

View File

@ -0,0 +1,3 @@
module golang.source-fellows.com/samples/http-server-test
go 1.14

View 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)
}

View 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())
}
})
}
}

View File

@ -0,0 +1,12 @@
package main
import (
"fmt"
"golang.source-fellows.com/samples/unittest/math"
)
func main() {
res := math.Add(1, 2)
fmt.Printf("Das Ergbnis ist %v", res)
}

3
quality/unittest/go.mod Normal file
View File

@ -0,0 +1,3 @@
module golang.source-fellows.com/samples/unittest
go 1.13

View File

@ -0,0 +1,6 @@
package math
// Add adds two integer values.
func Add(a int, b int) int {
return a + b
}

View File

@ -0,0 +1,42 @@
package math_test
import (
"testing"
"time"
"golang.source-fellows.com/samples/unittest/math"
)
func TestAdd(t *testing.T) {
res := math.Add(1, 2)
if res != 3 {
t.Fatalf("Not the expected result %v", res)
}
}
var testdata = []struct {
name string
param1 int
param2 int
result int
}{
{"positiv1", 1, 2, 3},
{"positiv2", 2, 3, 5},
{"negativ1", -2, 3, 5}, //stimmt nicht! - zur Demo von Testfehlern
{"negativ2", 2, -3, 7}, //stimmt nicht! - zur Demo von Testfehlern
}
func TestTableAdd(t *testing.T) {
for _, tt := range testdata {
t.Run(tt.name, func(t *testing.T) {
tt := tt
t.Parallel()
result := math.Add(tt.param1, tt.param2)
time.Sleep(time.Second * 1)
if result != tt.result {
t.Errorf("%v not expected. Expected: %v", result, tt.result)
}
})
}
}