@@ -4,12 +4,20 @@ import (
44 "encoding/hex"
55 "fmt"
66 "io"
7+ "net/http"
78 "os"
9+ "strings"
810 "testing"
911
1012 "gotest.tools/v3/assert"
1113)
1214
15+ type roundTripperFunc func (* http.Request ) (* http.Response , error )
16+
17+ func (f roundTripperFunc ) RoundTrip (req * http.Request ) (* http.Response , error ) {
18+ return f (req )
19+ }
20+
1321// The Bitcoin mainnet genesis block. See packages/btcindexer/src/api/put-blocks.test.ts
1422const blockHex = "0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000"
1523
@@ -45,11 +53,71 @@ func TestPutBlocksInt(t *testing.T) {
4553
4654 pb := PutBlock {Network : NetworkRegtest , Height : 156 , Block : blockBz }
4755
48- c := NewClient ("http://localhost:8787" )
56+ c := NewClient ("http://localhost:8787" , "" )
4957 resp , err := c .PutBlocks (PutBlocksReq {pb })
5058 assert .NilError (t , err )
5159 respBody , err := io .ReadAll (resp .Body )
5260 assert .NilError (t , err )
5361 fmt .Println (string (respBody ))
5462 assert .Equal (t , resp .StatusCode , 200 )
5563}
64+
65+ func TestClientPutBlocksAuthorizationHeader (t * testing.T ) {
66+ tests := []struct {
67+ name string
68+ token string
69+ wantAuth string
70+ }{
71+ {
72+ name : "no auth token omits Authorization header" ,
73+ token : "" ,
74+ wantAuth : "" ,
75+ },
76+ {
77+ name : "non-empty auth token sets Authorization header" ,
78+ token : "my-secret-token" ,
79+ wantAuth : "Bearer my-secret-token" ,
80+ },
81+ }
82+
83+ for _ , tt := range tests {
84+ t .Run (tt .name , func (t * testing.T ) {
85+ var capturedReq * http.Request
86+ rt := roundTripperFunc (func (req * http.Request ) (* http.Response , error ) {
87+ capturedReq = req
88+
89+ return & http.Response {
90+ StatusCode : http .StatusOK ,
91+ Body : io .NopCloser (strings .NewReader (`{}` )),
92+ Header : make (http.Header ),
93+ Request : req ,
94+ }, nil
95+ })
96+
97+ httpClient := http.Client {
98+ Transport : rt ,
99+ }
100+
101+ client := NewClient ("http://localhost:8787" , tt .token )
102+ client .c = httpClient
103+
104+ _ , err := client .PutBlocks (PutBlocksReq {})
105+ assert .NilError (t , err )
106+
107+ if capturedReq == nil {
108+ t .Fatalf ("expected request to be captured" )
109+ }
110+
111+ gotAuth := capturedReq .Header .Get ("Authorization" )
112+ if tt .wantAuth == "" {
113+ if gotAuth != "" {
114+ t .Fatalf ("expected no Authorization header, got %q" , gotAuth )
115+ }
116+ } else {
117+ if gotAuth != tt .wantAuth {
118+ t .Fatalf ("expected Authorization header %q, got %q" , tt .wantAuth , gotAuth )
119+ }
120+ }
121+ })
122+ }
123+ }
0 commit comments