|
| 1 | +// Copyright 2017 Google Inc. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package iid |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "net/http" |
| 20 | + "net/http/httptest" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "google.golang.org/api/option" |
| 24 | + |
| 25 | + "firebase.google.com/go/internal" |
| 26 | + |
| 27 | + "golang.org/x/net/context" |
| 28 | +) |
| 29 | + |
| 30 | +var testIIDConfig = &internal.InstanceIDConfig{ |
| 31 | + ProjectID: "test-project", |
| 32 | + Opts: []option.ClientOption{ |
| 33 | + option.WithTokenSource(&internal.MockTokenSource{AccessToken: "test-token"}), |
| 34 | + }, |
| 35 | +} |
| 36 | + |
| 37 | +func TestNoProjectID(t *testing.T) { |
| 38 | + client, err := NewClient(context.Background(), &internal.InstanceIDConfig{}) |
| 39 | + if client != nil || err == nil { |
| 40 | + t.Errorf("NewClient() = (%v, %v); want = (nil, error)", client, err) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestInvalidInstanceID(t *testing.T) { |
| 45 | + ctx := context.Background() |
| 46 | + client, err := NewClient(ctx, testIIDConfig) |
| 47 | + if err != nil { |
| 48 | + t.Fatal(err) |
| 49 | + } |
| 50 | + |
| 51 | + if err := client.DeleteInstanceID(ctx, ""); err == nil { |
| 52 | + t.Errorf("DeleteInstanceID(empty) = nil; want error") |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestDeleteInstanceID(t *testing.T) { |
| 57 | + var tr *http.Request |
| 58 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 59 | + tr = r |
| 60 | + w.Header().Set("Content-Type", "application/json") |
| 61 | + w.Write([]byte("{}")) |
| 62 | + })) |
| 63 | + defer ts.Close() |
| 64 | + |
| 65 | + ctx := context.Background() |
| 66 | + client, err := NewClient(ctx, testIIDConfig) |
| 67 | + if err != nil { |
| 68 | + t.Fatal(err) |
| 69 | + } |
| 70 | + client.endpoint = ts.URL |
| 71 | + if err := client.DeleteInstanceID(ctx, "test-iid"); err != nil { |
| 72 | + t.Errorf("DeleteInstanceID() = %v; want nil", err) |
| 73 | + } |
| 74 | + |
| 75 | + if tr == nil { |
| 76 | + t.Fatalf("Request = nil; want non-nil") |
| 77 | + } |
| 78 | + if tr.Method != "DELETE" { |
| 79 | + t.Errorf("Method = %q; want = %q", tr.Method, "DELETE") |
| 80 | + } |
| 81 | + if tr.URL.Path != "/project/test-project/instanceId/test-iid" { |
| 82 | + t.Errorf("Path = %q; want = %q", tr.URL.Path, "/project/test-project/instanceId/test-iid") |
| 83 | + } |
| 84 | + if h := tr.Header.Get("Authorization"); h != "Bearer test-token" { |
| 85 | + t.Errorf("Authorization = %q; want = %q", h, "Bearer test-token") |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestDeleteInstanceIDError(t *testing.T) { |
| 90 | + status := 200 |
| 91 | + var tr *http.Request |
| 92 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 93 | + tr = r |
| 94 | + w.WriteHeader(status) |
| 95 | + w.Header().Set("Content-Type", "application/json") |
| 96 | + w.Write([]byte("{}")) |
| 97 | + })) |
| 98 | + defer ts.Close() |
| 99 | + |
| 100 | + ctx := context.Background() |
| 101 | + client, err := NewClient(ctx, testIIDConfig) |
| 102 | + if err != nil { |
| 103 | + t.Fatal(err) |
| 104 | + } |
| 105 | + client.endpoint = ts.URL |
| 106 | + |
| 107 | + for k, v := range errorCodes { |
| 108 | + status = k |
| 109 | + err := client.DeleteInstanceID(ctx, "test-iid") |
| 110 | + if err == nil { |
| 111 | + t.Fatal("DeleteInstanceID() = nil; want = error") |
| 112 | + } |
| 113 | + |
| 114 | + want := fmt.Sprintf("instance id %q: %s", "test-iid", v) |
| 115 | + if err.Error() != want { |
| 116 | + t.Errorf("DeleteInstanceID() = %v; want = %v", err, want) |
| 117 | + } |
| 118 | + |
| 119 | + if tr == nil { |
| 120 | + t.Fatalf("Request = nil; want non-nil") |
| 121 | + } |
| 122 | + if tr.Method != "DELETE" { |
| 123 | + t.Errorf("Method = %q; want = %q", tr.Method, "DELETE") |
| 124 | + } |
| 125 | + if tr.URL.Path != "/project/test-project/instanceId/test-iid" { |
| 126 | + t.Errorf("Path = %q; want = %q", tr.URL.Path, "/project/test-project/instanceId/test-iid") |
| 127 | + } |
| 128 | + if h := tr.Header.Get("Authorization"); h != "Bearer test-token" { |
| 129 | + t.Errorf("Authorization = %q; want = %q", h, "Bearer test-token") |
| 130 | + } |
| 131 | + tr = nil |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func TestDeleteInstanceIDUnexpectedError(t *testing.T) { |
| 136 | + var tr *http.Request |
| 137 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 138 | + tr = r |
| 139 | + w.WriteHeader(511) |
| 140 | + w.Header().Set("Content-Type", "application/json") |
| 141 | + w.Write([]byte("{}")) |
| 142 | + })) |
| 143 | + defer ts.Close() |
| 144 | + |
| 145 | + ctx := context.Background() |
| 146 | + client, err := NewClient(ctx, testIIDConfig) |
| 147 | + if err != nil { |
| 148 | + t.Fatal(err) |
| 149 | + } |
| 150 | + client.endpoint = ts.URL |
| 151 | + |
| 152 | + err = client.DeleteInstanceID(ctx, "test-iid") |
| 153 | + if err == nil { |
| 154 | + t.Fatal("DeleteInstanceID() = nil; want = error") |
| 155 | + } |
| 156 | + |
| 157 | + want := "http error status: 511; reason: {}" |
| 158 | + if err.Error() != want { |
| 159 | + t.Errorf("DeleteInstanceID() = %v; want = %v", err, want) |
| 160 | + } |
| 161 | + |
| 162 | + if tr == nil { |
| 163 | + t.Fatalf("Request = nil; want non-nil") |
| 164 | + } |
| 165 | + if tr.Method != "DELETE" { |
| 166 | + t.Errorf("Method = %q; want = %q", tr.Method, "DELETE") |
| 167 | + } |
| 168 | + if tr.URL.Path != "/project/test-project/instanceId/test-iid" { |
| 169 | + t.Errorf("Path = %q; want = %q", tr.URL.Path, "/project/test-project/instanceId/test-iid") |
| 170 | + } |
| 171 | + if h := tr.Header.Get("Authorization"); h != "Bearer test-token" { |
| 172 | + t.Errorf("Authorization = %q; want = %q", h, "Bearer test-token") |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +func TestDeleteInstanceIDConnectionError(t *testing.T) { |
| 177 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 178 | + // Do nothing |
| 179 | + })) |
| 180 | + ts.Close() |
| 181 | + |
| 182 | + ctx := context.Background() |
| 183 | + client, err := NewClient(ctx, testIIDConfig) |
| 184 | + if err != nil { |
| 185 | + t.Fatal(err) |
| 186 | + } |
| 187 | + client.endpoint = ts.URL |
| 188 | + if err := client.DeleteInstanceID(ctx, "test-iid"); err == nil { |
| 189 | + t.Errorf("DeleteInstanceID() = nil; want = error") |
| 190 | + return |
| 191 | + } |
| 192 | +} |
0 commit comments