httpblock_test.go raw

   1  package httpblock
   2  
   3  import (
   4  	"context"
   5  	"net/http"
   6  	"net/http/httptest"
   7  	"testing"
   8  
   9  	"git.smesh.lol/nostrino/pkg/crypto/chainhash"
  10  )
  11  
  12  // TestABlockComesBackFromTheService: one GET by hash returns the bytes.
  13  func TestABlockComesBackFromTheService(t *testing.T) {
  14  	hash := chainhash.HashH([]byte("block"))
  15  
  16  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  17  		if r.URL.Path != "/block/btc/"+hash.String() {
  18  			http.NotFound(w, r)
  19  
  20  			return
  21  		}
  22  
  23  		_, _ = w.Write([]byte{1, 2, 3, 4})
  24  	}))
  25  
  26  	defer server.Close()
  27  
  28  	source, err := New(Config{BaseURL: server.URL, Chain: "btc"})
  29  
  30  	if err != nil {
  31  		t.Fatal(err)
  32  	}
  33  
  34  	raw, err := source(context.Background(), hash)
  35  
  36  	if err != nil {
  37  		t.Fatal(err)
  38  	}
  39  
  40  	if len(raw) != 4 || raw[0] != 1 {
  41  		t.Fatalf("block came back as %v", raw)
  42  	}
  43  }
  44  
  45  // TestAMissIsNilNotAnError: a 404 means the cache does not hold it, and the tunnel answers.
  46  func TestAMissIsNilNotAnError(t *testing.T) {
  47  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
  48  		http.NotFound(w, nil)
  49  	}))
  50  
  51  	defer server.Close()
  52  
  53  	source, err := New(Config{BaseURL: server.URL, Chain: "blake"})
  54  
  55  	if err != nil {
  56  		t.Fatal(err)
  57  	}
  58  
  59  	raw, err := source(context.Background(), chainhash.HashH([]byte("missing")))
  60  
  61  	if err != nil {
  62  		t.Fatalf("a miss was reported as an error: %v", err)
  63  	}
  64  
  65  	if raw != nil {
  66  		t.Fatalf("a miss returned %d bytes", len(raw))
  67  	}
  68  }
  69  
  70  // TestAServerFaultIsAnError: a service that answers 500 is broken, and that is worth a line.
  71  func TestAServerFaultIsAnError(t *testing.T) {
  72  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
  73  		http.Error(w, "boom", http.StatusInternalServerError)
  74  	}))
  75  
  76  	defer server.Close()
  77  
  78  	source, err := New(Config{BaseURL: server.URL, Chain: "btc"})
  79  
  80  	if err != nil {
  81  		t.Fatal(err)
  82  	}
  83  
  84  	if _, err := source(context.Background(), chainhash.HashH([]byte("x"))); err == nil {
  85  		t.Fatal("a 500 from the service was treated as a miss")
  86  	}
  87  }
  88  
  89  // TestABaseURLThatIsNotHTTPIsRefused: the source is configured, not discovered, so a typo is
  90  // caught where it is made.
  91  func TestABaseURLThatIsNotHTTPIsRefused(t *testing.T) {
  92  	if _, err := New(Config{BaseURL: "ftp://example", Chain: "btc"}); err == nil {
  93  		t.Fatal("a non-http base URL was accepted")
  94  	}
  95  
  96  	if _, err := New(Config{BaseURL: "http://example", Chain: ""}); err == nil {
  97  		t.Fatal("a source with no chain was accepted")
  98  	}
  99  }
 100