redone the test and go codes
All checks were successful
ci-tests/golang-test/pipeline/head This commit looks good
continuous-integration/drone Build is passing

This commit is contained in:
2020-12-22 17:45:36 +02:00
parent 9a81e18f47
commit dabf3c3281
2 changed files with 24 additions and 27 deletions

17
main.go
View File

@@ -1,20 +1,7 @@
package main
import (
"log"
"net/http"
)
type Server struct{}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"message": "Hello world"}`))
}
import "fmt"
func main() {
s := &Server{}
http.Handle("/", s)
log.Fatal(http.ListenAndServe(":8080", nil))
fmt.Println("Hello World")
}

View File

@@ -1,20 +1,30 @@
package main
import (
"log"
"net/http"
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
)
type Server struct{}
func TestHelloWorld(t *testing.T) {
rescueStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"message": "hello world"}`))
}
main()
func main() {
s := &Server{}
http.Handle("/", s)
log.Fatal(http.ListenAndServe(":8080", nil))
w.Close()
out, _ := ioutil.ReadAll(r)
os.Stdout = rescueStdout
fmt.Println(string(out))
var outputString string = string(out)
if strings.TrimSpace(outputString) != "Hello World" {
t.Errorf("Expected %s, got %s", "Hello World", out)
}
}