func TestStatusItemHandlerNoItemId(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, `{"access_token":"test","token_type":"bearer","expires_in":3600}`) })) defer ts.Close() tokenStore = tokenstore.NewTokenStore() tokenStore.Refresher(ts.URL, "", "") defer close(tokenStore.Refresh) req, err := http.NewRequest("GET", "/status/item/", nil) if err != nil { t.Fatal(err) } w := httptest.NewRecorder() statusItemHandler(w, req) if w.Code != http.StatusBadRequest { t.Errorf("Status handler didn't error %v when no item id provided", http.StatusBadRequest) } if w.Body.String() != "Error, you need to provide an ItemID. /status/item/[ItemID]\n" { t.Error("Status handler didn't return the correct information when no item id provided") } }
func TestStatusItemHandlerGoodResponseFromSierra(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, `{"access_token":"test","token_type":"bearer","expires_in":3600}`) })) defer ts.Close() tokenStore = tokenstore.NewTokenStore() tokenStore.Refresher(ts.URL, "", "") defer close(tokenStore.Refresh) ts2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, `{"id":2536252,"updatedDate":"2014-09-19T03:09:16Z","createdDate":"2007-05-11T18:37:00Z","deleted":false,"bibIds":[2401597],"location":{"code":"flr4 ","name":"Floor 4 Books"},"status":{"code":"-","display":"IN LIBRARY"},"barcode":"12016135026","callNumber":"|aJC578.R383|bG67 2007"}`) })) defer ts2.Close() //Get statusBibIDHandler to look at our mocked server oldAPIURL := *apiURL *apiURL = ts2.URL defer func() { *apiURL = oldAPIURL }() req, err := http.NewRequest("GET", "/status/item/2401597", nil) if err != nil { t.Fatal(err) } w := httptest.NewRecorder() statusItemHandler(w, req) if w.Code != http.StatusOK { t.Errorf("Status handler didn't return %v when provided with a good response.", http.StatusBadRequest) } }
func TestRawHandlerTestRewrite(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, `{"access_token":"test","token_type":"bearer","expires_in":3600}`) })) defer ts.Close() tokenStore = tokenstore.NewTokenStore() tokenStore.Refresher(ts.URL, "", "") defer close(tokenStore.Refresh) req, err := http.NewRequest("GET", "/raw/?bibIds=1234", nil) if err != nil { t.Fatal(err) } oldAPIURL := *apiURL *apiURL = "http://apiurl.com/test/" defer func() { *apiURL = oldAPIURL }() rawRewriter(req) if req.URL.String() != "http://apiurl.com/test?bibIds=1234" { t.Error("The raw handler is not correctly rewriting the url") } }
func TestStatusBibHandlerBadURLParse(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, `{"access_token":"test","token_type":"bearer","expires_in":3600}`) })) defer ts.Close() tokenStore = tokenstore.NewTokenStore() tokenStore.Refresher(ts.URL, "", "") defer close(tokenStore.Refresh) r, err := http.NewRequest("GET", "/status/bib/2401597", nil) if err != nil { t.Fatal(err) } w := httptest.NewRecorder() oldAPIURL := *apiURL *apiURL = ":" defer func() { *apiURL = oldAPIURL }() statusBibHandler(w, r) if w.Code != http.StatusInternalServerError { t.Error("Should have returned InternalServerError") } }
func TestGetTokenOrErrorFailTokenStoreTimeout(t *testing.T) { tokenStore = tokenstore.NewTokenStore() r, err := http.NewRequest("GET", "", nil) if err != nil { t.Fatal(err) } w := httptest.NewRecorder() time.Sleep(time.Second * 31) if _, err := getTokenOrError(w, r); err == nil { t.Error("Should have failed with unparseable URL.") } if w.Code != http.StatusInternalServerError { t.Error("Should have returned a InternalServerError") } }
func TestGetTokenOrErrorFailTokenStoreUninitialized(t *testing.T) { tokenStore = tokenstore.NewTokenStore() tokenStore.Refresher(":", "", "") defer close(tokenStore.Refresh) r, err := http.NewRequest("GET", "", nil) if err != nil { t.Fatal(err) } w := httptest.NewRecorder() if _, err := getTokenOrError(w, r); err == nil { t.Error("Should have failed with unparseable URL.") } if w.Code != http.StatusInternalServerError { t.Error("Should have returned a InternalServerError") } }
apiURL = flag.String("url", sierraapi.DefaultURL, "API url.") certFile = flag.String("certfile", "", "Certificate file location.") keyFile = flag.String("keyfile", "", "Private key file location.") clientKey = flag.String("key", "", "Client Key") clientSecret = flag.String("secret", "", "Client Secret") headerACAO = flag.String("acaoheader", DefaultACAOHeader, "Access-Control-Allow-Origin Header for CORS. Multiple origins separated by ;") raw = flag.Bool("raw", DefaultRawAccess, "Allow access to the raw Sierra API under /raw/") newLimit = flag.Int("newlimit", 16, "The number of items to serve from the /new endpoint.") logFileLocation = flag.String("logfile", l.DefaultLogFileLocation, "Log file. By default, log messages will be printed to stdout.") logMaxSize = flag.Int("logmaxsize", l.DefaultLogMaxSize, "The maximum size of log files before they are rotated, in megabytes.") logMaxBackups = flag.Int("logmaxbackups", l.DefaultLogMaxBackups, "The maximum number of old log files to keep.") logMaxAge = flag.Int("logmaxage", l.DefaultLogMaxAge, "The maximum number of days to retain old log files, in days.") logLevel = flag.String("loglevel", "warn", "The maximum log level which will be logged. error < warn < info < debug < trace. For example, trace will log everything, info will log info, warn, and error.") tokenStore = tokenstore.NewTokenStore() ) func init() { flag.Usage = func() { fmt.Fprint(os.Stderr, "Tyro: A helper for Sierra APIs\nVersion 0.7.8\n\n") flag.PrintDefaults() fmt.Fprintln(os.Stderr, " The possible environment variables:") flag.VisitAll(func(f *flag.Flag) { uppercaseName := strings.ToUpper(f.Name) fmt.Fprintf(os.Stderr, " %v%v\n", EnvPrefix, uppercaseName) }) fmt.Fprintln(os.Stderr, "If a certificate file is provided, Tyro will attempt to use HTTPS.")