serve_test.go raw

   1  package bridge
   2  
   3  import (
   4  	"net/http"
   5  	"net/http/httptest"
   6  	"strings"
   7  	"testing"
   8  )
   9  
  10  func TestComposeHandler(t *testing.T) {
  11  	handler := ComposeHandler()
  12  
  13  	req := httptest.NewRequest("GET", "/compose", nil)
  14  	rec := httptest.NewRecorder()
  15  
  16  	handler(rec, req)
  17  
  18  	if rec.Code != http.StatusOK {
  19  		t.Fatalf("status = %d, want 200", rec.Code)
  20  	}
  21  
  22  	ct := rec.Header().Get("Content-Type")
  23  	if !strings.Contains(ct, "text/html") {
  24  		t.Errorf("Content-Type = %q, want text/html", ct)
  25  	}
  26  
  27  	body := rec.Body.String()
  28  	if !strings.Contains(body, "Marmot Email Bridge") {
  29  		t.Error("compose page missing title")
  30  	}
  31  	if !strings.Contains(body, "copyToClipboard") {
  32  		t.Error("compose page missing JS function")
  33  	}
  34  }
  35  
  36  func TestDecryptHandler(t *testing.T) {
  37  	handler := DecryptHandler()
  38  
  39  	req := httptest.NewRequest("GET", "/decrypt", nil)
  40  	rec := httptest.NewRecorder()
  41  
  42  	handler(rec, req)
  43  
  44  	if rec.Code != http.StatusOK {
  45  		t.Fatalf("status = %d, want 200", rec.Code)
  46  	}
  47  
  48  	ct := rec.Header().Get("Content-Type")
  49  	if !strings.Contains(ct, "text/html") {
  50  		t.Errorf("Content-Type = %q, want text/html", ct)
  51  	}
  52  
  53  	body := rec.Body.String()
  54  	if !strings.Contains(body, "Decrypt") {
  55  		t.Error("decrypt page missing title")
  56  	}
  57  	if !strings.Contains(body, "ChaCha20") {
  58  		t.Error("decrypt page missing crypto reference")
  59  	}
  60  }
  61