1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- 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
- app.loadLanguages()
- 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)
- }
- }
|