func TestAPIRouter(t *testing.T) {
	var tests = []struct {
		code   int
		method string
		url    string
	}{
		{200, "GET", "/api/v0/albums"},
		{200, "GET", "/api/v0/albums/1"},
		{200, "GET", "/api/v0/albums?limit=0,10"},
		{400, "GET", "/api/v999/albums"},
		{400, "GET", "/api/v0/albums/foo"},
		{400, "GET", "/api/v0/albums?limit=0"},
		{400, "GET", "/api/v0/albums?limit=foo,bar"},
		{404, "GET", "/api/v0/albums/99999999"},
		{400, "GET", "/api/v999/art"},
		{400, "GET", "/api/v0/art"},
		{400, "GET", "/api/v0/art/foo"},
		{404, "GET", "/api/v0/art/99999999"},
		{200, "GET", "/api/v0/artists"},
		{200, "GET", "/api/v0/artists/1"},
		{200, "GET", "/api/v0/artists?limit=0,10"},
		{400, "GET", "/api/v999/artists"},
		{400, "GET", "/api/v0/artists/foo"},
		{400, "GET", "/api/v0/artists?limit=0"},
		{400, "GET", "/api/v0/artists?limit=foo,bar"},
		{404, "GET", "/api/v0/artists/99999999"},
		{200, "GET", "/api/v0/folders"},
		{200, "GET", "/api/v0/folders/1"},
		{400, "GET", "/api/v999/folders"},
		{200, "GET", "/api/v0/folders?limit=0,10"},
		{400, "GET", "/api/v0/folders/foo"},
		{400, "GET", "/api/v0/folders?limit=0"},
		{400, "GET", "/api/v0/folders?limit=foo,bar"},
		{404, "GET", "/api/v0/folders/99999999"},
		{400, "POST", "/api/v999/lastfm"},
		{400, "POST", "/api/v0/lastfm"},
		{400, "POST", "/api/v0/lastfm/foo"},
		{400, "POST", "/api/v0/lastfm/login"},
		{401, "POST", "/api/v0/lastfm/nowplaying"},
		{401, "POST", "/api/v0/lastfm/scrobble"},
		{200, "GET", "/api/v0/search/foo"},
		{400, "GET", "/api/v999/search"},
		{400, "GET", "/api/v0/search"},
		{200, "GET", "/api/v0/songs"},
		{200, "GET", "/api/v0/songs/1"},
		{200, "GET", "/api/v0/songs?limit=0,10"},
		{200, "GET", "/api/v0/songs?random=10"},
		{400, "GET", "/api/v999/songs"},
		{400, "GET", "/api/v0/songs/foo"},
		{400, "GET", "/api/v0/songs?limit=0"},
		{400, "GET", "/api/v0/songs?limit=foo,bar"},
		{400, "GET", "/api/v0/songs?random=foo"},
		{404, "GET", "/api/v0/songs/99999999"},
		{200, "GET", "/api/v0/status"},
		{200, "GET", "/api/v0/status?metrics=all"},
		{400, "GET", "/api/v999/status"},
		{400, "GET", "/api/v999/stream"},
		{400, "GET", "/api/v0/stream"},
		{400, "GET", "/api/v0/stream/foo"},
		{404, "GET", "/api/v0/stream/99999999"},
		{400, "GET", "/api/v999/transcode"},
		{400, "GET", "/api/v0/transcode"},
		{400, "GET", "/api/v0/transcode/foo"},
		{404, "GET", "/api/v0/transcode/99999999"},
		{503, "GET", "/api/v0/transcode/1"},
		{200, "GET", "/api/v0/users"},
		{200, "GET", "/api/v0/users/1"},
		{400, "GET", "/api/v0/users/foo"},
		{404, "GET", "/api/v0/users/99999999"},
	}
	for _, test := range tests {
		req, err := http.NewRequest(test.method, "http://localhost:8080"+test.url, nil)
		if err != nil {
			t.Fatalf("Failed to create HTTP request")
		}
		context.Set(req, api.CtxRender, r)
		context.Set(req, api.CtxUser, &data.User{RoleID: 1})
		context.Set(req, api.CtxSession, new(data.Session))
		w := httptest.NewRecorder()
		n.ServeHTTP(w, req)
		if w.Code != test.code {
			t.Fatalf("HTTP [%v != %v] %s %s", w.Code, test.code, test.method, test.url)
		}
		body, err := ioutil.ReadAll(w.Body)
		if err != nil {
			t.Fatal(err)
		}
		var errRes api.ErrorResponse
		if err := json.Unmarshal(body, &errRes); err != nil {
			log.Println(string(body))
			t.Fatal(err)
		}
		if errRes.Error != nil && errRes.Error.Code != test.code {
			t.Fatalf("Body [%v != %v] %s %s", errRes.Error.Code, test.code, test.method, test.url)
		}
		log.Printf("OK: [%d] %s %s", test.code, test.method, test.url)
	}
}
TestAPIRouter verifies that all API request processing functionality is working properly
package core
import (
	"encoding/json"
	"io/ioutil"
	"log"
	"net/http"
	"net/http/httptest"
	"os"
	"testing"
	"github.com/mdlayher/wavepipe/api"
	"github.com/mdlayher/wavepipe/data"
	"github.com/codegangsta/negroni"
	"github.com/gorilla/context"
	"github.com/unrolled/render"
)
var n = negroni.New()
var r = render.New(render.Options{})
func init() {
	data.DB = new(data.SqliteBackend)
	data.DB.DSN("~/.config/wavepipe/wavepipe.db")
	if err := data.DB.Open(); err != nil {
		os.Exit(1)
	}
	n.UseHandler(newRouter())
}