github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/api/http_client_test.go (about)

     1  package api
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"encoding/json"
     7  	"io/ioutil"
     8  	"net/http"
     9  	"net/http/httptest"
    10  	"net/url"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/google/go-cmp/cmp"
    15  	golog "github.com/ipfs/go-log"
    16  	"github.com/qri-io/qri/base/dsfs"
    17  	testcfg "github.com/qri-io/qri/config/test"
    18  	"github.com/qri-io/qri/dsref"
    19  	"github.com/qri-io/qri/event"
    20  	"github.com/qri-io/qri/lib"
    21  	qhttp "github.com/qri-io/qri/lib/http"
    22  	"github.com/qri-io/qri/p2p"
    23  	"github.com/qri-io/qri/repo/test"
    24  )
    25  
    26  func TestHTTPClient(t *testing.T) {
    27  	if err := confirmQriNotRunning(); err != nil {
    28  		t.Skip(err.Error())
    29  	}
    30  
    31  	ctx, cancel := context.WithCancel(context.Background())
    32  	defer cancel()
    33  
    34  	prevXformVer := APIVersion
    35  	APIVersion = "test_version"
    36  	defer func() {
    37  		APIVersion = prevXformVer
    38  	}()
    39  
    40  	// bump up log level to keep test output clean
    41  	golog.SetLogLevel("qriapi", "error")
    42  	defer golog.SetLogLevel("qriapi", "info")
    43  
    44  	// to keep hashes consistent, artificially specify the timestamp by overriding
    45  	// the dsfs.Timestamp func
    46  	prev := dsfs.Timestamp
    47  	defer func() { dsfs.Timestamp = prev }()
    48  	dsfs.Timestamp = func() time.Time { return time.Date(2001, 01, 01, 01, 01, 01, 01, time.UTC) }
    49  
    50  	r, err := test.NewTestRepo()
    51  	if err != nil {
    52  		t.Fatalf("error allocating test repo: %s", err.Error())
    53  	}
    54  
    55  	// Cannot use TestRunner because we need to set cfg.API.ReadOnly.
    56  	// TODO(dlong): Add a testRunner call trace that does this correctly.
    57  	cfg := testcfg.DefaultConfigForTesting()
    58  
    59  	node, err := p2p.NewQriNode(r, cfg.P2P, event.NilBus, nil)
    60  	if err != nil {
    61  		t.Fatal(err.Error())
    62  	}
    63  	// TODO (b5) - hack until tests have better instance-generation primitives
    64  	inst := lib.NewInstanceFromConfigAndNode(ctx, cfg, node)
    65  	s := New(inst)
    66  
    67  	server := httptest.NewServer(NewServerRoutes(s))
    68  	sURL, err := url.Parse(server.URL)
    69  	if err != nil {
    70  		t.Fatal(err.Error())
    71  	}
    72  
    73  	httpClient, err := qhttp.NewClient(cfg.API.Address)
    74  	if err != nil {
    75  		t.Fatal(err.Error())
    76  	}
    77  
    78  	// override with test URI
    79  	httpClient.Address = sURL.Host
    80  	httpClient.Protocol = "http"
    81  
    82  	if err = httpClient.CallRaw(ctx, AEHome, "", nil, &bytes.Buffer{}); err != nil {
    83  		t.Fatal(err.Error())
    84  	}
    85  
    86  	res := []dsref.VersionInfo{}
    87  	p := lib.CollectionListParams{}
    88  	err = httpClient.CallMethod(ctx, qhttp.AEList, http.MethodPost, "", p, &res)
    89  	if err != nil {
    90  		t.Fatal(err.Error())
    91  	}
    92  
    93  	expectBytes, err := ioutil.ReadFile("testdata/http_client/list.json")
    94  	if err != nil {
    95  		t.Fatalf("error reading expected bytes: %s", err)
    96  	}
    97  	var expect []dsref.VersionInfo
    98  	if err := json.Unmarshal(expectBytes, &expect); err != nil {
    99  		t.Fatal(err)
   100  	}
   101  
   102  	t.Skip("TODO(b5): collection update has broken this contract. fix both test & collection implementation")
   103  	if diff := cmp.Diff(expect, res); diff != "" {
   104  		t.Errorf("byte mismatch (-want +got):\n%s", diff)
   105  	}
   106  }