Skip to content

Commit

Permalink
folio: fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
miku committed Oct 6, 2021
1 parent 97ac76d commit 73893b9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
12 changes: 9 additions & 3 deletions folio/folio.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Package folio add support for a minimal subset of the FOLIO library
// platform API.
package folio

import (
Expand All @@ -7,6 +9,7 @@ import (
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"time"

Expand All @@ -28,7 +31,7 @@ type API struct {
Base string
Tenant string // e.g. "de_15"
Client Doer
Token string // will be populated by API.Authenticate(...)
Token string
}

func New() *API {
Expand Down Expand Up @@ -79,7 +82,7 @@ func (api *API) Authenticate(username, password string) (err error) {
if resp.StatusCode != 201 {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("failed to read body: %v", err)
log.Printf("[ee] failed to read body: %v", err)
} else {
log.Println(string(b))
}
Expand Down Expand Up @@ -122,7 +125,10 @@ func (api *API) MetadataCollections(opts MetadataCollectionsOpts) (*MetadataColl
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return nil, fmt.Errorf("api returned: %v", resp.Status)
b, _ := httputil.DumpResponse(resp, true)
log.Printf("[ee] --------\n%s\n", string(b))
log.Println("[ee] --------")
return nil, fmt.Errorf("[ee] api returned: %v", resp.Status)
}
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, err
Expand Down
7 changes: 6 additions & 1 deletion folio/folio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

func TestAuthenticate(t *testing.T) {
token := "1234"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/bl-users/login" {
t.Fatalf("unexpected URL: %s", r.URL.Path)
Expand All @@ -17,7 +18,8 @@ func TestAuthenticate(t *testing.T) {
t.Fatalf("cannot dump request")
}
t.Logf("server got request: %v", string(b))
w.Header().Add("X-OKAPI-TOKEN", "12345678")
w.Header().Add("X-OKAPI-TOKEN", token)
w.WriteHeader(http.StatusCreated)
}))
t.Logf("test server: %v", ts.URL)
defer func() {
Expand All @@ -33,4 +35,7 @@ func TestAuthenticate(t *testing.T) {
if err != nil {
t.Fatalf("failed with: %v", err)
}
if api.Token != token {
t.Fatalf("got %v, want %v", api.Token, token)
}
}

0 comments on commit 73893b9

Please sign in to comment.