Files
vm/api/server/router_test.go
zeekayandHanzo Dev e9e9fb6315 Give the node's HTTP server a home beside the VMs it serves
api/server was an earlier copy of node/server/http that had drifted behind it.
Its exported surface was a strict subset of the node's, so this replaces the
contents rather than adding a second package: a second server would have meant
two Server interfaces, two New, two routers, and no answer to which one a chain
should mount on.

What arrives with it is the part api/server never had. Mount is the estate's
only projection from a zip App to an http.Handler, and it installs Authorize
before it serves, so an app cannot reach the router without the node's
authorization decision on it. Chain builds the one address a chain answers at,
and chain_test walks this module's own source to hold every caller to it. The
ZAP-RPC listener and the /v1 prefix come across as well; api/server was still
serving /ext.

Two files stay behind because they already have homes here. The node's
traced_handler duplicates api.TraceHandler, which this package was already
calling, so server.go keeps calling it and takes luxfi/trace in place of the
node-local trace shim.

Two tests do not come across, and neither is dropped from the node. mount_test
builds a real Z-Chain from luxfi/chains, which requires luxfi/vm, so importing
it here would point the layering back at itself. probe_test needs the Ops app
on node/service/health, which api/health does not carry. Both keep running
where they are.

constants moves to v1.6.4 to agree with the node on the chain segment. The two
modules disagreed, v1.6.2 still saying bc where the node says chain, and the
address Chain builds has to be the address the node's router serves.

newMetrics now reports a duplicate registration instead of returning a
half-built set, which is what TestMetricsRegistrationFailure had been asking
for and failing to get. That test stays, next to the node's, because the two
ask different questions: one registry twice, or two registries once.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-06 12:21:19 -07:00

61 lines
1.3 KiB
Go

// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package server
import (
"net/http"
"testing"
"github.com/stretchr/testify/require"
)
type testHandler struct{ called bool }
func (t *testHandler) ServeHTTP(_ http.ResponseWriter, _ *http.Request) {
t.called = true
}
func TestAliasing(t *testing.T) {
require := require.New(t)
r := newRouter()
require.NoError(r.AddAlias("1", "2", "3"))
require.NoError(r.AddAlias("1", "4"))
require.NoError(r.AddAlias("5", "1"))
require.NoError(r.AddAlias("3", "6"))
err := r.AddAlias("7", "4")
require.ErrorIs(err, errAlreadyReserved)
handler1 := &testHandler{}
err = r.AddRouter("2", "", handler1)
require.ErrorIs(err, errAlreadyReserved)
require.NoError(r.AddRouter("5", "", handler1))
handler, exists := r.routes["5"][""]
require.True(exists)
require.Equal(handler1, handler)
require.NoError(r.AddAlias("5", "7"))
handler, exists = r.routes["7"][""]
require.True(exists)
require.Equal(handler1, handler)
handler, err = r.GetHandler("7", "")
require.NoError(err)
require.Equal(handler1, handler)
}
func TestBlock(t *testing.T) {
require := require.New(t)
r := newRouter()
require.NoError(r.AddAlias("1", "1"))
handler1 := &testHandler{}
err := r.AddRouter("1", "", handler1)
require.ErrorIs(err, errAlreadyReserved)
}