Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bcessa committed Dec 17, 2019
1 parent b63ede3 commit f7bedd8
Show file tree
Hide file tree
Showing 4 changed files with 235 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Makefile
@@ -1,6 +1,6 @@
.DEFAULT_GOAL := help
.PHONY: all
VERSION_TAG=0.1.0
VERSION_TAG=0.1.1
BINARY_NAME=govanity
DOCKER_IMAGE=govanity

Expand Down
178 changes: 178 additions & 0 deletions config_test.go
@@ -0,0 +1,178 @@
// nolint: gocognit
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"testing"

"gopkg.in/yaml.v2"
)

var sampleConf string = `
host: go.bryk.io
cache_max_age: 3600
paths:
sample:
repo: https://github.com/bryk-io/sample
vcs: git
`

func TestNewServerConfig(t *testing.T) {
// Empty configuration
conf := NewServerConfig()
if len(conf.Paths) != 0 {
t.Error("configuration should be empty by default")
}

// Load YAML content
if err := yaml.Unmarshal([]byte(sampleConf), conf); err != nil {
t.Error(err)
}
if conf.Paths["sample"].VCS.String() != "git" {
t.Error("failed to decode VCS value")
}

// Re-encode in JSON format
_, err := json.MarshalIndent(conf, "", " ")
if err != nil {
t.Error(err)
}
}

func TestServer(t *testing.T) {
// Load sample configuration
conf := NewServerConfig()
_ = yaml.Unmarshal([]byte(sampleConf), conf)

// Dummy build values
coreVersion = "0.1.0"
buildCode = "foo-bar"

// Start test server
h := newHandler(conf)
server := http.Server{Addr: ":9091", Handler: getServerMux(h)}
go func() {
_ = server.ListenAndServe()
}()

t.Run("api/version", func(t *testing.T) {
res, err := http.Get("http://localhost:9091/api/version")
if err != nil {
t.Error(err)
return
}
defer func() {
_ = res.Body.Close()
}()

if res.Header.Get("content-type") != "application/json" {
t.Error("invalid content type")
}
if res.StatusCode != http.StatusOK {
t.Error("invalid status code")
}
if res.Header.Get("X-Content-Type-Options") != "nosniff" {
t.Error("invalid content options")
}
if res.Header.Get("X-Go-Vanity-Server-Version") != "0.1.0" {
t.Error("missing version header")
}
if res.Header.Get("X-Go-Vanity-Server-Build") != "foo-bar" {
t.Error("missing build code header")
}
})

t.Run("api/conf", func(t *testing.T) {
res, err := http.Get("http://localhost:9091/api/conf")
if err != nil {
t.Error(err)
return
}
defer func() {
_ = res.Body.Close()
}()
if res.Header.Get("content-type") != "application/json" {
t.Error("invalid content type")
}
if res.StatusCode != http.StatusOK {
t.Error("invalid status code")
}
})

t.Run("api/ping", func(t *testing.T) {
res, err := http.Get("http://localhost:9091/api/ping")
if err != nil {
t.Error(err)
return
}
defer func() {
_ = res.Body.Close()
}()
if res.StatusCode != http.StatusOK {
t.Error("invalid status code")
}
response, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Error(err)
}
if fmt.Sprintf("%s", response) != "pong" {
t.Error("invalid response")
}
})

t.Run("index.html", func(t *testing.T) {
res, err := http.Get("http://localhost:9091/index.html")
if err != nil {
t.Error(err)
return
}
defer func() {
_ = res.Body.Close()
}()
if res.StatusCode != http.StatusOK {
t.Error("invalid status code")
}
})

t.Run("invalid-path", func(t *testing.T) {
res, err := http.Get("http://localhost:9091/invalid-path")
if err != nil {
t.Error(err)
return
}
defer func() {
_ = res.Body.Close()
}()
if res.StatusCode != http.StatusNotFound {
t.Error("invalid status code")
}
response, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Error(err)
}
if fmt.Sprintf("%s", response) != "unknown path" {
t.Error("invalid response")
}
})

t.Run("valid-path", func(t *testing.T) {
res, err := http.Get("http://localhost:9091/sample")
if err != nil {
t.Error(err)
return
}
defer func() {
_ = res.Body.Close()
}()
if res.StatusCode != http.StatusOK {
t.Error("invalid status code")
}
})

defer func() {
_ = server.Close()
}()
}
4 changes: 2 additions & 2 deletions helm/govanity/Chart.yaml
Expand Up @@ -2,6 +2,6 @@ apiVersion: v2
name: govanity
description: Go vanity URLs server
type: application
version: 0.1.0
appVersion: 0.1.0
version: 0.1.1
appVersion: 0.1.1
home: https://github.com/bryk-io/go-vanity
98 changes: 54 additions & 44 deletions main.go
Expand Up @@ -16,8 +16,6 @@ import (
"gopkg.in/yaml.v2"
)

var cacheValue string

func main() {
// Validate arguments
file, port := getParameters()
Expand Down Expand Up @@ -52,48 +50,10 @@ func main() {
os.Exit(-1)
}

// Prepare server mux
h := newHandler(conf)
cacheValue = h.cache()
mux := http.NewServeMux()
mux.HandleFunc("/api/ping", func(res http.ResponseWriter, req *http.Request) {
setHeaders(res, "text/plain; charset=utf-8", http.StatusOK)
_, _ = res.Write([]byte("pong"))
})
mux.HandleFunc("/api/version", func(res http.ResponseWriter, req *http.Request) {
js, _ := json.MarshalIndent(versionInfo(), "", " ")
setHeaders(res, "application/json", http.StatusOK)
_, _ = res.Write(js)
})
mux.HandleFunc("/api/conf", func(res http.ResponseWriter, req *http.Request) {
js, _ := json.MarshalIndent(conf, "", " ")
setHeaders(res, "application/json", http.StatusOK)
_, _ = res.Write(js)
})
mux.HandleFunc("/index.html", func(res http.ResponseWriter, req *http.Request) {
index, err := h.getIndex()
if err != nil {
setHeaders(res, "text/plain; charset=utf-8", http.StatusInternalServerError)
_, _ = res.Write([]byte(err.Error()))
return
}
setHeaders(res, "text/html; charset=utf-8", http.StatusOK)
_, _ = res.Write(index)
})
mux.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
repo, err := h.getRepo(strings.TrimSuffix(req.URL.Path, "/"))
if err != nil {
setHeaders(res, "text/plain; charset=utf-8", http.StatusNotFound)
_, _ = res.Write([]byte(err.Error()))
return
}
setHeaders(res, "text/html; charset=utf-8", http.StatusOK)
_, _ = res.Write(repo)
})

// Start server
h := newHandler(conf)
fmt.Println("serving on port:", port)
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), mux); err != nil {
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), getServerMux(h)); err != nil {
fmt.Println("server error: ", err)
os.Exit(-1)
}
Expand Down Expand Up @@ -129,11 +89,61 @@ func getParameters() (string, int) {
return file, port
}

func setHeaders(res http.ResponseWriter, ct string, code int) {
res.Header().Add("Cache-Control", cacheValue)
func setHeaders(res http.ResponseWriter, ct string, cache string, code int) {
res.Header().Add("Cache-Control", cache)
res.Header().Add("Content-Type", ct)
res.Header().Add("X-Content-Type-Options", "nosniff")
res.Header().Add("X-Go-Vanity-Server-Build", buildCode)
res.Header().Add("X-Go-Vanity-Server-Version", coreVersion)
res.WriteHeader(code)
}

func getServerMux(h *handler) *http.ServeMux {
mux := http.NewServeMux()

// Ping
mux.HandleFunc("/api/ping", func(res http.ResponseWriter, req *http.Request) {
setHeaders(res, "text/plain; charset=utf-8", h.cache(), http.StatusOK)
_, _ = res.Write([]byte("pong"))
})

// Version
mux.HandleFunc("/api/version", func(res http.ResponseWriter, req *http.Request) {
js, _ := json.MarshalIndent(versionInfo(), "", " ")
setHeaders(res, "application/json", h.cache(), http.StatusOK)
_, _ = res.Write(js)
})

// Configuration
mux.HandleFunc("/api/conf", func(res http.ResponseWriter, req *http.Request) {
js, _ := json.MarshalIndent(h.conf, "", " ")
setHeaders(res, "application/json", h.cache(), http.StatusOK)
_, _ = res.Write(js)
})

// Main index
mux.HandleFunc("/index.html", func(res http.ResponseWriter, req *http.Request) {
index, err := h.getIndex()
if err != nil {
setHeaders(res, "text/plain; charset=utf-8", h.cache(), http.StatusInternalServerError)
_, _ = res.Write([]byte(err.Error()))
return
}
setHeaders(res, "text/html; charset=utf-8", h.cache(), http.StatusOK)
_, _ = res.Write(index)
})

// Catch-all path
mux.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
repo, err := h.getRepo(strings.TrimSuffix(req.URL.Path, "/"))
if err != nil {
setHeaders(res, "text/plain; charset=utf-8", h.cache(), http.StatusNotFound)
_, _ = res.Write([]byte(err.Error()))
return
}
setHeaders(res, "text/html; charset=utf-8", h.cache(), http.StatusOK)
_, _ = res.Write(repo)
})

return mux
}

0 comments on commit f7bedd8

Please sign in to comment.