Skip to content

Commit 3fa0d5d

Browse files
committed
itest: add UI main page fallback test
1 parent 17b0a7a commit 3fa0d5d

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

itest/litd_mode_integrated_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88
"encoding/hex"
99
"fmt"
1010
"io/ioutil"
11+
"net/http"
1112
"strings"
1213
"testing"
14+
"time"
1315

1416
"github.com/btcsuite/btcutil"
1517
"github.com/lightninglabs/faraday/frdrpc"
@@ -27,6 +29,13 @@ import (
2729
"gopkg.in/macaroon.v2"
2830
)
2931

32+
const (
33+
// indexHtmlMarker is a string that appears in the rendered index.html
34+
// file of the main UI. This is created by Webpack and should be fairly
35+
// stable.
36+
indexHtmlMarker = "webpackJsonplightning-terminal"
37+
)
38+
3039
// requestFn is a function type for a helper function that makes a daemon
3140
// specific request and returns the response and error for it. This is used to
3241
// abstract away the lnd/faraday/loop/pool specific gRPC code from the actual
@@ -52,6 +61,10 @@ var (
5261
InsecureSkipVerify: true,
5362
},
5463
}
64+
client = http.Client{
65+
Transport: transport,
66+
Timeout: 1 * time.Second,
67+
}
5568

5669
lndRequestFn = func(ctx context.Context,
5770
c grpc.ClientConnInterface) (proto.Message, error) {
@@ -240,6 +253,10 @@ func testModeIntegrated(net *NetworkHarness, t *harnessTest) {
240253
})
241254
}
242255
})
256+
257+
t.t.Run("UI index page fallback", func(tt *testing.T) {
258+
runIndexPageCheck(tt, net.Alice.Cfg.LitAddr())
259+
})
243260
}
244261

245262
// runCertificateCheck checks that the TLS certificates presented to clients are
@@ -375,6 +392,40 @@ func runUIPasswordCheck(t *testing.T, hostPort, tlsCertPath, uiPassword string,
375392
require.Contains(t, string(json), successContent)
376393
}
377394

395+
// runIndexPageCheck makes sure the index page is returned correctly.
396+
func runIndexPageCheck(t *testing.T, hostPort string) {
397+
body, err := getURL(fmt.Sprintf("https://%s/index.html", hostPort))
398+
require.NoError(t, err)
399+
require.Contains(t, body, indexHtmlMarker)
400+
401+
// The UI implements "virtual" pages by using the browser history API.
402+
// Any URL that looks like a directory should fall back to the main
403+
// index.html file as well.
404+
body, err = getURL(fmt.Sprintf("https://%s/loop", hostPort))
405+
require.NoError(t, err)
406+
require.Contains(t, body, indexHtmlMarker)
407+
}
408+
409+
// getURL retrieves the body of a given URL, ignoring any TLS certificate the
410+
// server might present.
411+
func getURL(url string) (string, error) {
412+
resp, err := client.Get(url)
413+
if err != nil {
414+
return "", err
415+
}
416+
417+
defer func() {
418+
_ = resp.Body.Close()
419+
}()
420+
421+
body, err := ioutil.ReadAll(resp.Body)
422+
if err != nil {
423+
return "", err
424+
}
425+
426+
return string(body), nil
427+
}
428+
378429
// getServerCertificates returns the TLS certificates that a server presents to
379430
// clients.
380431
func getServerCertificates(hostPort string) ([]*x509.Certificate, error) {

0 commit comments

Comments
 (0)