summaryrefslogtreecommitdiff
path: root/tools/block-generator/generator/server_test.go
blob: a885181dddc2ff9ef665bb48da73d75f7f58d096 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (C) 2019-2023 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package generator

import (
	"fmt"
	"strings"
	"testing"

	"github.com/algorand/go-algorand/test/partitiontest"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestParseURL(t *testing.T) {
	partitiontest.PartitionTest(t)
	const blockQueryPrefix = "http://v2/blocks/"
	const accountQueryPrefix = "http://v2/accounts/"
	const deltaQueryPrefix = "http://v2/deltas/"
	var testcases = []struct {
		name          string
		url           string
		expectedParam string
		err           string
	}{
		{
			name:          "no block",
			url:           "/v2/blocks/",
			expectedParam: "",
			err:           "invalid request path, /v2/blocks/",
		},
		{
			name:          "normal one digit",
			url:           fmt.Sprintf("%s1", blockQueryPrefix),
			expectedParam: "1",
			err:           "",
		},
		{
			name:          "normal long number",
			url:           fmt.Sprintf("%s12345678", blockQueryPrefix),
			expectedParam: "12345678",
			err:           "",
		},
		{
			name:          "with query parameters",
			url:           fmt.Sprintf("%s1234?pretty", blockQueryPrefix),
			expectedParam: "1234",
			err:           "",
		},
		{
			name:          "with query parameters",
			url:           fmt.Sprintf("%s1234?pretty", blockQueryPrefix),
			expectedParam: "1234",
			err:           "",
		},
		{
			name:          "no deltas",
			url:           "/v2/deltas/",
			expectedParam: "",
			err:           "invalid request path, /v2/deltas/",
		},
		{
			name:          "deltas",
			url:           fmt.Sprintf("%s123?Format=msgp", deltaQueryPrefix),
			expectedParam: "123",
			err:           "",
		},
		{
			name:          "no account",
			url:           "/v2/accounts/",
			expectedParam: "",
			err:           "invalid request path, /v2/accounts/",
		},
		{
			name:          "accounts",
			url:           fmt.Sprintf("%sAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGFFWAF4", accountQueryPrefix),
			expectedParam: "AIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGFFWAF4",
			err:           "",
		},
	}

	for _, tc := range testcases {
		tc := tc
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()
			round, err := parseURL(tc.url)
			if len(tc.err) == 0 {
				msg := fmt.Sprintf("Unexpected error parsing '%s', expected round '%s' received error: %v",
					tc.url, tc.expectedParam, err)
				require.NoError(t, err, msg)
				assert.Equal(t, tc.expectedParam, round)
			} else {
				require.Error(t, err, fmt.Sprintf("Expected an error containing: %s", tc.err))
				require.True(t, strings.Contains(err.Error(), tc.err))
			}
		})
	}
}