12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- package main
- import (
- "fmt"
- "net/http"
- "net/http/httptest"
- "strings"
- "testing"
- )
- func fakeServer(statusCode int, data string) *httptest.Server {
- function := func(w http.ResponseWriter, r *http.Request) {
- w.Header().Set("Content-Type", "application/json")
- w.WriteHeader(statusCode)
- fmt.Fprint(w, data)
- }
- return httptest.NewServer(http.HandlerFunc(function))
- }
- func TestSetBaseURL(t *testing.T) {
- toTest := []string{"http://test.com:880/test/", "http://test.com:880/test"}
- var app Application
- for _, testCase := range toTest {
- app.setBaseURL(testCase)
- if strings.HasSuffix(app.BaseURL, "//") || !strings.HasSuffix(app.BaseURL, "/") {
- t.Errorf("URL has been malformed: %s", app.BaseURL)
- }
- }
- }
- func TestLoadLanguages(t *testing.T) {
- var app Application
- if !app.loadLanguages() {
- t.Fatal("Couldn't load languages!")
- }
- testLanguageCode := "dan"
- testLanguage := "Danish"
- fromMap, ok := app.Languages[testLanguageCode]
- if !ok {
- t.Fatalf("There's no key '%s'!", testLanguageCode)
- }
- if fromMap != testLanguage {
- t.Fatalf("Value of key '%s' isn't '%s'", testLanguageCode, testLanguage)
- }
- }
- func TestLogin(t *testing.T) {
- loginResponse := `{
- "Name": "admin",
- "Sid": "00000000-0000-0000-0001-000000000001",
- "AccessToken": "fde0f7ed-d585-48ec-a0a9-397aea195ccd"
- }`
- server := fakeServer(http.StatusOK, loginResponse)
- defer server.Close()
- var app Application
- app.setBaseURL(server.URL)
- status, err := app.login()
- if !status || err != nil {
- t.Fatalf("Status: %v, error: %s", status, err)
- }
- }
- func TestLoginBadURL(t *testing.T) {
- var app Application
- app.setBaseURL("badURL")
- status, err := app.login()
- if status || err == nil {
- t.Fatalf("Status: %v, error: %s", status, err)
- }
- }
- func TestLoginWrongStatus(t *testing.T) {
- server := fakeServer(http.StatusBadRequest, "")
- defer server.Close()
- var app Application
- app.setBaseURL(server.URL)
- status, err := app.login()
- if status || err == nil {
- t.Fatalf("Status: %v, error: %s", status, err)
- }
- }
|