...

Source file src/github.com/go-kivik/kivik/v4/x/kivikd/couchserver/favicon_test.go

Documentation: github.com/go-kivik/kivik/v4/x/kivikd/couchserver

     1  // Licensed under the Apache License, Version 2.0 (the "License"); you may not
     2  // use this file except in compliance with the License. You may obtain a copy of
     3  // the License at
     4  //
     5  //  http://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     9  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    10  // License for the specific language governing permissions and limitations under
    11  // the License.
    12  
    13  //go:build !js
    14  
    15  package couchserver
    16  
    17  import (
    18  	"bytes"
    19  	"io"
    20  	"net/http"
    21  	"net/http/httptest"
    22  	"testing"
    23  )
    24  
    25  func TestFavicoDefault(t *testing.T) {
    26  	h := &Handler{}
    27  	w := httptest.NewRecorder()
    28  	req := httptest.NewRequest("GET", "/favicon.ico", nil)
    29  	handler := h.GetFavicon()
    30  	handler(w, req)
    31  	resp := w.Result()
    32  	defer resp.Body.Close()
    33  	buf := &bytes.Buffer{}
    34  	if _, err := buf.ReadFrom(resp.Body); err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	embedded, err := files.Open("files/favicon.ico")
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	expected, err := io.ReadAll(embedded)
    42  	if err != nil {
    43  		t.Fatal(err)
    44  	}
    45  	if !bytes.Equal(buf.Bytes(), expected) {
    46  		t.Errorf("Unexpected file contents. %d bytes instead of %d", buf.Len(), len(expected))
    47  	}
    48  }
    49  
    50  func TestFavicoNotFound(t *testing.T) {
    51  	h := &Handler{Favicon: "some/invalid/path"}
    52  	w := httptest.NewRecorder()
    53  	req := httptest.NewRequest("GET", "/favicon.ico", nil)
    54  	handler := h.GetFavicon()
    55  	handler(w, req)
    56  	resp := w.Result()
    57  	_ = resp.Body.Close()
    58  	if resp.StatusCode != http.StatusNotFound {
    59  		t.Errorf("Expected status: %d, actual: %d", http.StatusNotFound, resp.StatusCode)
    60  	}
    61  }
    62  
    63  func TestFavicoExternalFile(t *testing.T) {
    64  	h := &Handler{Favicon: "./files/favicon.ico"}
    65  	w := httptest.NewRecorder()
    66  	req := httptest.NewRequest("GET", "/favicon.ico", nil)
    67  	handler := h.GetFavicon()
    68  	handler(w, req)
    69  	resp := w.Result()
    70  	defer resp.Body.Close()
    71  	buf := &bytes.Buffer{}
    72  	if _, err := buf.ReadFrom(resp.Body); err != nil {
    73  		t.Fatal(err)
    74  	}
    75  	embedded, err := files.Open("files/favicon.ico")
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  	expected, err := io.ReadAll(embedded)
    80  	if err != nil {
    81  		t.Fatal(err)
    82  	}
    83  	if !bytes.Equal(buf.Bytes(), expected) {
    84  		t.Errorf("Unexpected file contents. %d bytes instead of %d", buf.Len(), len(expected))
    85  	}
    86  }
    87  

View as plain text