diff --git a/http_client_test.go b/http_client_test.go new file mode 100644 index 0000000000000000000000000000000000000000..b739d9a0b2b8974ec558c43c714b2156d5eeb9c5 --- /dev/null +++ b/http_client_test.go @@ -0,0 +1,110 @@ +package federation +// +// GangGo Diaspora Federation Library +// Copyright (C) 2017 Lukas Matt +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// + +import ( + "fmt" + "net/http" + "net/http/httptest" + "testing" + "encoding/xml" +) + +type Test struct { + XMLName xml.Name `xml:"AB";json:"-"` + A string `xml:"A";json:"A"` + B string `xml:"B";json:"B"` +} + +func TestPushToPrivate(t *testing.T) { + var guid = "1234" + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + if r.URL.Path != "/receive/users/" + guid { + t.Errorf("%s", r.URL.Path) + } + })) + defer ts.Close() + + err := PushToPrivate(ts.URL[7:], guid, nil) + if err != nil { + t.Errorf("Some error occured while sending: %v", err) + } + + err = PushToPrivate("", guid, nil) + if err == nil { + t.Errorf("Expected an error, got nil") + } +} + +func TestPushToPublic(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + if r.URL.Path != "/receive/public" { + t.Errorf("%s", r.URL.Path) + } + })) + defer ts.Close() + + err := PushToPublic(ts.URL[7:], nil) + if err != nil { + t.Errorf("Some error occured while sending: %v", err) + } + + err = PushToPublic("", nil) + if err == nil { + t.Errorf("Expected an error, got nil") + } +} + +func TestFetchJson(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + fmt.Fprintln(w, `{"A":"a","B":"b"}`) + })) + defer ts.Close() + + var res Test + err := FetchJson("GET", ts.URL, nil, &res) + if err != nil { + t.Errorf("Some error occured while sending: %v", err) + } + + if res.A != "a" || res.B != "b" { + t.Errorf("Expected to be a and b, got %s and %s", res.A, res.B) + } +} + +func TestFetchXml(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + fmt.Fprintln(w, `ab`) + })) + defer ts.Close() + + var res Test + err := FetchXml("GET", ts.URL, nil, &res) + if err != nil { + t.Errorf("Some error occured while sending: %v", err) + } + + if res.A != "a" || res.B != "b" { + t.Errorf("Expected to be a and b, got %s and %s", res.A, res.B) + } +} diff --git a/magic_test.go b/magic_test.go index de42dcb1e4882daca0d5bd81ed89317bcac5e35e..9b032db1d1c9b557cbac89cb18ad421b67652337 100644 --- a/magic_test.go +++ b/magic_test.go @@ -38,6 +38,11 @@ func TestMagicEnvelope(t *testing.T) { t.Errorf("Expected to be %s, got %s", string(TEST_MAGIC_PAYLOAD), string(payload)) } + + _, err = MagicEnvelope("", string(TEST_AUTHOR), TEST_MAGIC_DATA) + if err == nil { + t.Errorf("Expected to be an error, got nil") + } } func TestEncryptedMagicEnvelope(t *testing.T) { @@ -57,4 +62,18 @@ func TestEncryptedMagicEnvelope(t *testing.T) { if !matched { t.Errorf("Expected match for pattern '%s', got nothing", pattern) } + + _, err = EncryptedMagicEnvelope( + "", string(TEST_PUB_KEY), + TEST_AUTHOR, TEST_MAGIC_DATA) + if err == nil { + t.Errorf("Expected to be an error, got nil") + } + + _, err = EncryptedMagicEnvelope( + string(TEST_PRIV_KEY), "", + TEST_AUTHOR, TEST_MAGIC_DATA) + if err == nil { + t.Errorf("Expected to be an error, got nil") + } } diff --git a/salmon_test.go b/salmon_test.go index d276681863bccf4e88c31a6f05fe2d0f04564983..a8c2636db96ba9a0ef75457c50feb4eb21da8716 100644 --- a/salmon_test.go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5gPUZZVJt9f6kn9HoGQ56MX2n46KdKKid8=`) - message, err := ParseDecryptedRequest(xml) + message, err := ParseDecryptedRequest([]byte("")) + if err == nil { + t.Errorf("Expected to be an error, got nil") + } + + message, err = ParseDecryptedRequest([]byte("