|
| 1 | +package httpx |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestIsURL(t *testing.T) { |
| 9 | + tests := []struct { |
| 10 | + url string |
| 11 | + ok bool |
| 12 | + }{ |
| 13 | + {"https://antonz.org/sqlpkg.json", true}, |
| 14 | + {"https://github.com/nalgeon/sqlpkg/raw/main/pkg/sqlite/stmt.json", true}, |
| 15 | + {"https://raw.githubusercontent.com/nalgeon/sqlpkg/main/pkg/sqlite/stmt.json", true}, |
| 16 | + {"./testdata/sqlpkg.json", false}, |
| 17 | + {"/Users/anton/sqlpkg.json", false}, |
| 18 | + {"file:///Users/anton/sqlpkg.json", false}, |
| 19 | + } |
| 20 | + for _, test := range tests { |
| 21 | + ok := IsURL(test.url) |
| 22 | + if ok != test.ok { |
| 23 | + t.Errorf("IsURL(%s): expected %v, got %v", test.url, test.ok, ok) |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +func TestHostname(t *testing.T) { |
| 29 | + tests := []struct { |
| 30 | + url string |
| 31 | + host string |
| 32 | + }{ |
| 33 | + {"https://antonz.org/sqlpkg.json", "antonz.org"}, |
| 34 | + {"https://github.com/nalgeon/sqlpkg/raw/main/pkg/sqlite/stmt.json", "github.com"}, |
| 35 | + {"https://raw.githubusercontent.com/nalgeon/sqlpkg/main/pkg/sqlite/stmt.json", "raw.githubusercontent.com"}, |
| 36 | + {"./testdata/sqlpkg.json", ""}, |
| 37 | + {"/Users/anton/sqlpkg.json", ""}, |
| 38 | + {"file:///Users/anton/sqlpkg.json", ""}, |
| 39 | + } |
| 40 | + for _, test := range tests { |
| 41 | + host := Hostname(test.url) |
| 42 | + if host != test.host { |
| 43 | + t.Errorf("Hostname(%s): expected %v, got %v", test.url, test.host, host) |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestExists(t *testing.T) { |
| 49 | + srv := MockServer() |
| 50 | + defer srv.Close() |
| 51 | + |
| 52 | + t.Run("exists", func(t *testing.T) { |
| 53 | + ok := Exists(srv.URL + "/sqlpkg.json") |
| 54 | + if !ok { |
| 55 | + t.Errorf("Exists: unexpected %v", ok) |
| 56 | + } |
| 57 | + }) |
| 58 | + t.Run("does not exist", func(t *testing.T) { |
| 59 | + ok := Exists(srv.URL + "/missing.json") |
| 60 | + if ok { |
| 61 | + t.Errorf("Exists: unexpected %v", ok) |
| 62 | + } |
| 63 | + }) |
| 64 | +} |
| 65 | + |
| 66 | +func TestGetBody(t *testing.T) { |
| 67 | + srv := MockServer() |
| 68 | + defer srv.Close() |
| 69 | + |
| 70 | + t.Run("success", func(t *testing.T) { |
| 71 | + body, err := GetBody(srv.URL+"/example.txt", "text/plain") |
| 72 | + if err != nil { |
| 73 | + t.Errorf("GetBody: unexpected error %v", err) |
| 74 | + } |
| 75 | + defer body.Close() |
| 76 | + |
| 77 | + data, err := io.ReadAll(body) |
| 78 | + if err != nil { |
| 79 | + t.Errorf("io.ReadAll: unexpected error %v", err) |
| 80 | + } |
| 81 | + |
| 82 | + if string(data) != "example.txt" { |
| 83 | + t.Errorf("GetBody: unexpected value %q", string(data)) |
| 84 | + } |
| 85 | + }) |
| 86 | + t.Run("failure", func(t *testing.T) { |
| 87 | + _, err := GetBody(srv.URL+"/missing.txt", "text/plain") |
| 88 | + if err == nil { |
| 89 | + t.Error("GetBody: expected error, got nil") |
| 90 | + } |
| 91 | + }) |
| 92 | +} |
| 93 | + |
| 94 | +func TestGetBytes(t *testing.T) { |
| 95 | + srv := MockServer() |
| 96 | + defer srv.Close() |
| 97 | + |
| 98 | + t.Run("success", func(t *testing.T) { |
| 99 | + data, err := GetBytes(srv.URL + "/example.txt") |
| 100 | + if err != nil { |
| 101 | + t.Errorf("GetBytes: unexpected error %v", err) |
| 102 | + } |
| 103 | + if string(data) != "example.txt" { |
| 104 | + t.Errorf("GetBytes: unexpected value %q", string(data)) |
| 105 | + } |
| 106 | + }) |
| 107 | + t.Run("failure", func(t *testing.T) { |
| 108 | + _, err := GetBytes(srv.URL + "/missing.txt") |
| 109 | + if err == nil { |
| 110 | + t.Error("GetBytes: expected error, got nil") |
| 111 | + } |
| 112 | + }) |
| 113 | +} |
| 114 | + |
| 115 | +func TestGetJSON(t *testing.T) { |
| 116 | + srv := MockServer() |
| 117 | + defer srv.Close() |
| 118 | + |
| 119 | + t.Run("success", func(t *testing.T) { |
| 120 | + type Example struct{ Body string } |
| 121 | + ex, err := GetJSON[Example](srv.URL + "/example.json") |
| 122 | + if err != nil { |
| 123 | + t.Errorf("GetJSON: unexpected error %v", err) |
| 124 | + } |
| 125 | + if ex.Body != "example.txt" { |
| 126 | + t.Errorf("GetJSON: unexpected value %q", ex.Body) |
| 127 | + } |
| 128 | + }) |
| 129 | + t.Run("failure", func(t *testing.T) { |
| 130 | + type Example struct{ Body string } |
| 131 | + _, err := GetJSON[Example](srv.URL + "/example.txt") |
| 132 | + if err == nil { |
| 133 | + t.Error("GetJSON: expected error, got nil") |
| 134 | + } |
| 135 | + }) |
| 136 | +} |
0 commit comments