Implemented version check

Updated readme
This commit is contained in:
Carlos Salguero
2017-01-11 15:01:33 -03:00
parent 3fd93b4f32
commit c2419ba10a
14 changed files with 681 additions and 130 deletions

View File

@@ -0,0 +1,58 @@
package versioncheck
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestCheckUpdates(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := ioutil.ReadAll(r.Body)
m := strings.Split(string(body), ";")
advices := []Advice{
Advice{
Hash: m[0],
ToolName: m[1],
Advice: "There is a new version",
},
}
buf, _ := json.Marshal(advices)
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, string(buf))
}))
defer ts.Close()
msg, err := CheckUpdates(ts.URL, "pt-test", "2.2.18")
if err != nil {
t.Errorf("error while checking %s", err)
}
if msg == "" {
t.Error("got empty response")
}
}
func TestEmptyResponse(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "")
}))
defer ts.Close()
msg, err := CheckUpdates(ts.URL, "pt-test", "2.2.18")
if err == nil {
t.Error("response should return error due to empty body")
}
if msg != "" {
t.Error("response should return error due to empty body")
}
}