| 1 |
// Copyright (c) 2022 Tulir Asokan |
| 2 |
// |
| 3 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
package mautrix_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
|
| 14 |
"maunium.net/go/mautrix" |
| 15 |
) |
| 16 |
|
| 17 |
func TestClient_BuildURL(t *testing.T) { |
| 18 |
cli, err := mautrix.NewClient("https://example.com", "", "") |
| 19 |
assert.NoError(t, err) |
| 20 |
assert.Equal(t, cli.HomeserverURL.Scheme, "https") |
| 21 |
assert.Equal(t, cli.HomeserverURL.Host, "example.com") |
| 22 |
assert.Equal(t, cli.HomeserverURL.Path, "") |
| 23 |
built := cli.BuildClientURL("v3", "foo/bar%2F🐈 1", "hello", "world") |
| 24 |
assert.Equal(t, "https://example.com/_matrix/client/v3/foo%2Fbar%252F%F0%9F%90%88%201/hello/world", built) |
| 25 |
} |
| 26 |
|
| 27 |
func TestClient_BuildURL_HTTP(t *testing.T) { |
| 28 |
cli, err := mautrix.NewClient("http://example.com", "", "") |
| 29 |
assert.NoError(t, err) |
| 30 |
assert.Equal(t, cli.HomeserverURL.Scheme, "http") |
| 31 |
assert.Equal(t, cli.HomeserverURL.Host, "example.com") |
| 32 |
assert.Equal(t, cli.HomeserverURL.Path, "") |
| 33 |
built := cli.BuildClientURL("v3", "foo/bar%2F🐈 1", "hello", "world") |
| 34 |
assert.Equal(t, "http://example.com/_matrix/client/v3/foo%2Fbar%252F%F0%9F%90%88%201/hello/world", built) |
| 35 |
} |
| 36 |
|
| 37 |
func TestClient_BuildURL_MissingScheme(t *testing.T) { |
| 38 |
cli, err := mautrix.NewClient("example.com", "", "") |
| 39 |
assert.NoError(t, err) |
| 40 |
assert.Equal(t, cli.HomeserverURL.Scheme, "https") |
| 41 |
assert.Equal(t, cli.HomeserverURL.Host, "example.com") |
| 42 |
assert.Equal(t, cli.HomeserverURL.Path, "") |
| 43 |
built := cli.BuildClientURL("v3", "foo/bar%2F🐈 1", "hello", "world") |
| 44 |
assert.Equal(t, "https://example.com/_matrix/client/v3/foo%2Fbar%252F%F0%9F%90%88%201/hello/world", built) |
| 45 |
} |
| 46 |
|
| 47 |
func TestClient_BuildURL_WithPath(t *testing.T) { |
| 48 |
cli, err := mautrix.NewClient("https://example.com/base", "", "") |
| 49 |
assert.NoError(t, err) |
| 50 |
assert.Equal(t, cli.HomeserverURL.Scheme, "https") |
| 51 |
assert.Equal(t, cli.HomeserverURL.Host, "example.com") |
| 52 |
assert.Equal(t, cli.HomeserverURL.Path, "/base") |
| 53 |
built := cli.BuildClientURL("v3", "foo/bar%2F🐈 1", "hello", "world") |
| 54 |
assert.Equal(t, "https://example.com/base/_matrix/client/v3/foo%2Fbar%252F%F0%9F%90%88%201/hello/world", built) |
| 55 |
} |
| 56 |
|
| 57 |
func TestClient_BuildURL_MissingSchemeWithPath(t *testing.T) { |
| 58 |
cli, err := mautrix.NewClient("example.com/base", "", "") |
| 59 |
assert.NoError(t, err) |
| 60 |
assert.Equal(t, cli.HomeserverURL.Scheme, "https") |
| 61 |
assert.Equal(t, cli.HomeserverURL.Host, "example.com") |
| 62 |
assert.Equal(t, cli.HomeserverURL.Path, "/base") |
| 63 |
built := cli.BuildClientURL("v3", "foo/bar%2F🐈 1", "hello", "world") |
| 64 |
assert.Equal(t, "https://example.com/base/_matrix/client/v3/foo%2Fbar%252F%F0%9F%90%88%201/hello/world", built) |
| 65 |
} |