summary refs log tree commit diff stats
path: root/init.go
Commit message (Expand)AuthorAgeFilesLines
* caching some static assetsBen Morrison2019-05-231-0/+13
* configuration init changesBen Morrison2019-05-231-88/+57
* runtime bugs re: http400 and database reading on startupBen Morrison2019-05-211-0/+8
* runtime bugs related to api outputBen Morrison2019-05-211-1/+1
* rename closeLog channelBen Morrison2019-05-211-5/+5
* missing mutex rlock for confobjBen Morrison2019-05-211-0/+2
* review of db functionsBen Morrison2019-05-211-10/+6
* database push/pull functionsBen Morrison2019-05-211-5/+27
* added cache update / db push intervals to conf;Ben Morrison2019-05-211-9/+86
* fleshed out POST handler, added remote registry listBen Morrison2019-05-211-0/+3
* endpoint query function addedBen Morrison2019-05-201-1/+7
* template initialization addedBen Morrison2019-05-131-9/+22
* fixed viper config parsing resulting in build errorsBen Morrison2019-05-131-2/+8
* help messageBen Morrison2019-05-131-1/+2
* changing flagsBen Morrison2019-05-131-8/+14
* handling viper error when binding to pflagsBen Morrison2019-05-131-1/+3
* watching for ^C. added comments.Ben Morrison2019-05-131-0/+25
* passing logfile as var to closing goroutine to prevent race conditionBen Morrison2019-05-121-9/+9
* added stdoutLogging bool and related configurationBen Morrison2019-05-121-26/+54
* config fleshed out; using viper+pflagBen Morrison2019-05-121-2/+76
* logging initialization in init()Ben Morrison2019-05-111-0/+27
3'>233 234 235 236 237 238 239 240 241
package svc // import "github.com/getwtxt/getwtxt/svc"

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"reflect"
	"testing"
)

// The first few are testing whether the landing page is
// being sent correctly. If i change the base behavior of
//    /api
//    /api/plain
// later, then I'll change the tests.

func basicHandlerTest(path string, name string, t *testing.T) {
	initTestConf()

	t.Run(name, func(t *testing.T) {
		w := httptest.NewRecorder()
		req := httptest.NewRequest("GET", path, nil)

		staticHandler(w, req)
		resp := w.Result()
		defer resp.Body.Close()

		if resp.StatusCode != http.StatusOK {
			t.Errorf(fmt.Sprintf("%v", resp.StatusCode))
		}

		bt, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			t.Errorf("%v\n", err)
		}
		if !reflect.DeepEqual(bt, staticCache.index) {
			t.Errorf("Byte mismatch\n")
		}
	})
}
func Test_indexHandler(t *testing.T) {
	basicHandlerTest("http://localhost"+testport+"/", "indexHandler", t)
}
func Benchmark_indexHandler(b *testing.B) {
	initTestConf()

	w := httptest.NewRecorder()
	req := httptest.NewRequest("GET", "http://localhost"+testport+"/", nil)
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		staticHandler(w, req)
	}
}
func Test_apiBaseHandler(t *testing.T) {
	basicHandlerTest("http://localhost"+testport+"/api", "apiBaseHandler", t)
}
func Test_apiFormatHandler(t *testing.T) {
	basicHandlerTest("http://localhost"+testport+"/api/format", "apiFormatHandler", t)
}

var endpointCases = []struct {
	name   string
	req    *http.Request
	status int
}{
	{
		name:   "Regular Query: /api/plain/users",
		req:    httptest.NewRequest("GET", "http://localhost"+testport+"/api/plain/users", nil),
		status: http.StatusOK,
	},
	{
		name:   "Regular Query: /api/plain/mentions",
		req:    httptest.NewRequest("GET", "http://localhost"+testport+"/api/plain/mentions", nil),
		status: http.StatusOK,
	},
	{
		name:   "Regular Query: /api/plain/tweets",
		req:    httptest.NewRequest("GET", "http://localhost"+testport+"/api/plain/tweets", nil),
		status: http.StatusOK,
	},
	{
		name:   "Invalid Endpoint: /api/plain/statuses",
		req:    httptest.NewRequest("GET", "http://localhost"+testport+"/api/plain/statuses", nil),
		status: http.StatusNotFound,
	},
}

func Test_apiEndpointHandler(t *testing.T) {
	initTestConf()
	mockRegistry()

	for _, tt := range endpointCases {

		t.Run(tt.name, func(t *testing.T) {
			w := httptest.NewRecorder()
			apiEndpointHandler(w, tt.req)

			resp := w.Result()
			defer resp.Body.Close()

			if resp.StatusCode != tt.status {
				t.Errorf(fmt.Sprintf("%v", resp.StatusCode))
			}

			if tt.status == http.StatusOK {
				var body []byte
				buf := bytes.NewBuffer(body)

				err := resp.Write(buf)
				if err != nil {
					t.Errorf("%v\n", err)
				}
				if buf == nil {
					t.Errorf("Got nil\n")
				}
				if len(buf.Bytes()) == 0 {
					t.Errorf("Got zero data\n")
				}
			}
		})
	}
}
func Benchmark_apiEndpointHandler(b *testing.B) {
	initTestConf()
	mockRegistry()

	w := httptest.NewRecorder()
	b.ResetTimer()

	for _, tt := range endpointCases {
		for i := 0; i < b.N; i++ {
			apiEndpointHandler(w, tt.req)
		}
	}
}

func Test_apiTagsBaseHandler(t *testing.T) {
	initTestConf()
	mockRegistry()

	t.Run("apiTagsBaseHandler", func(t *testing.T) {
		w := httptest.NewRecorder()
		req := httptest.NewRequest("GET", "http://localhost"+testport+"/api/plain/tags", nil)

		apiTagsBaseHandler(w, req)
		resp := w.Result()
		defer resp.Body.Close()

		if resp.StatusCode != http.StatusOK {
			t.Errorf(fmt.Sprintf("%v", resp.StatusCode))
		}

		bd, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			t.Errorf("%v\n", err)
		}
		if len(bd) == 0 {
			t.Errorf("Got no data from registry\n")
		}
	})
}
func Benchmark_apiTagsBaseHandler(b *testing.B) {
	initTestConf()
	mockRegistry()
	w := httptest.NewRecorder()
	r := httptest.NewRequest("GET", "http://localhost"+testport+"/api/plain/tags", nil)
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		apiTagsBaseHandler(w, r)
	}
}
func Test_apiTagsHandler(t *testing.T) {
	initTestConf()
	mockRegistry()

	t.Run("apiTagsHandler", func(t *testing.T) {
		w := httptest.NewRecorder()
		req := httptest.NewRequest("GET", "http://localhost"+testport+"/api/plain/tags/programming", nil)

		apiTagsHandler(w, req)
		resp := w.Result()
		defer resp.Body.Close()

		data, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			t.Errorf("%v\n", err)
		}

		if resp.StatusCode != http.StatusOK {
			t.Errorf(fmt.Sprintf("%v", resp.StatusCode))
		}
		if len(data) == 0 {
			t.Errorf("Got no data: %v\n", data)
		}
	})
}

func Benchmark_apiTagsHandler(b *testing.B) {
	initTestConf()
	mockRegistry()
	w := httptest.NewRecorder()
	r := httptest.NewRequest("GET", "http://localhost"+testport+"/api/plain/tags/programming", nil)
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		apiTagsHandler(w, r)
	}
}

func Test_cssHandler(t *testing.T) {
	initTestConf()

	name := "CSS Handler Test"
	css, err := ioutil.ReadFile("../assets/style.css")
	if err != nil {
		t.Errorf("Couldn't read ../assets/style.css: %v\n", err)
	}

	w := httptest.NewRecorder()
	req := httptest.NewRequest("GET", "http://localhost"+testport+"/css", nil)

	t.Run(name, func(t *testing.T) {
		staticHandler(w, req)

		resp := w.Result()
		defer resp.Body.Close()

		body, _ := ioutil.ReadAll(resp.Body)

		if resp.StatusCode != 200 {
			t.Errorf("cssHandler(): %v\n", resp.StatusCode)
		}
		if !reflect.DeepEqual(body, css) {
			t.Errorf("cssHandler(): Byte mismatch\n")
		}
	})
}