Add marketplace APIs (#43)

* Add marketplace APIs
This commit is contained in:
Thibault Maekelbergh
2021-02-18 15:16:09 +01:00
committed by GitHub
parent d77d28b101
commit 7704281b49
5 changed files with 178 additions and 5 deletions

76
marketplace_test.go Normal file
View File

@@ -0,0 +1,76 @@
package discogs
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strconv"
"testing"
)
const testReleaseID = 9893847
func MarketplaceServer(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
switch r.URL.Path {
case "/marketplace" + priceSuggestionsURI + strconv.Itoa(testReleaseID):
w.WriteHeader(http.StatusOK)
if _, err := io.WriteString(w, priceSuggestionJson); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
case "/marketplace" + releaseStatsURI + strconv.Itoa(testReleaseID):
w.WriteHeader(http.StatusOK)
if _, err := io.WriteString(w, releaseStatsJson); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
func TestMarketplacePriceSuggestions(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(MarketplaceServer))
defer ts.Close()
d := initDiscogsClient(t, &Options{URL: ts.URL})
suggestion, err := d.PriceSuggestions(testReleaseID)
if err != nil {
t.Fatalf("failed to get price suggestion: %s", err)
}
json, err := json.Marshal(suggestion)
if err != nil {
t.Fatalf("failed to marshal folder: %s", err)
}
compareJson(t, string(json), priceSuggestionJson)
}
func TestMarketplaceReleaseStatistics(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(MarketplaceServer))
defer ts.Close()
d := initDiscogsClient(t, &Options{URL: ts.URL})
stats, err := d.ReleaseStatistics(testReleaseID)
if err != nil {
t.Fatalf("failed to get price suggestion: %s", err)
}
json, err := json.Marshal(stats)
if err != nil {
t.Fatalf("failed to marshal folder: %s", err)
}
compareJson(t, string(json), releaseStatsJson)
}