http_client_test.go 4.71 KB
Newer Older
Lukas Matt's avatar
Lukas Matt committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
package federation
//
// GangGo Diaspora Federation Library
// Copyright (C) 2017 Lukas Matt <lukas@zauberstuhl.de>
//
// 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 <http://www.gnu.org/licenses/>.
//

import (
  "fmt"
  "net/http"
  "net/http/httptest"
  "testing"
  "encoding/xml"
Lukas Matt's avatar
Lukas Matt committed
26
  "io/ioutil"
Lukas Matt's avatar
Lukas Matt committed
27 28 29
)

type Test struct {
30 31 32
  XMLName xml.Name `xml:"AB" json:"-"`
  A string `xml:"A" json:"A"`
  B string `xml:"B" json:"B"`
Lukas Matt's avatar
Lukas Matt committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
}

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)
  }

Lukas Matt's avatar
Lukas Matt committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  err = FetchJson("GET", ts.URL, nil, nil)
  if err == nil {
    t.Errorf("Web request without result struct should throw an error")
  }

  // run without protocol
  err = FetchJson("GET", ts.URL[7:], nil, &res)
  if err != nil {
    t.Errorf("Some error occured while sending: %v", err)
  }

  err = FetchJson("GET", "https://" + ts.URL[7:], nil, &res)
  if err == nil {
    t.Errorf("HTTPS should fail since we do not have ssl enabled")
  }

Lukas Matt's avatar
Lukas Matt committed
106 107 108 109 110 111 112 113 114 115 116 117 118
  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><A>a</A><B>b</B></AB>`)
  }))
  defer ts.Close()

  var res Test
Lukas Matt's avatar
Lukas Matt committed
119 120 121 122 123 124 125 126 127 128 129 130
  err := FetchXml("GET", ts.URL, nil, nil)
  if err == nil {
    t.Errorf("Web request without result struct should throw an error")
  }

  err = FetchXml("GET", ts.URL, nil, &res)
  if err != nil {
    t.Errorf("Some error occured while sending: %v", err)
  }

  // run without protocol
  err = FetchXml("GET", ts.URL[7:], nil, &res)
Lukas Matt's avatar
Lukas Matt committed
131 132 133 134
  if err != nil {
    t.Errorf("Some error occured while sending: %v", err)
  }

Lukas Matt's avatar
Lukas Matt committed
135 136 137 138 139
  err = FetchXml("GET", "https://" + ts.URL[7:], nil, &res)
  if err == nil {
    t.Errorf("HTTPS should fail since we do not have ssl enabled")
  }

Lukas Matt's avatar
Lukas Matt committed
140 141 142 143
  if res.A != "a" || res.B != "b" {
    t.Errorf("Expected to be a and b, got %s and %s", res.A, res.B)
  }
}
Lukas Matt's avatar
Lukas Matt committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

func TestFetchHtml(t *testing.T) {
  expectedData := `<AB><A>a</A><B>b</B></AB>`
  ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(http.StatusOK)
    fmt.Fprintln(w, expectedData)
  }))
  defer ts.Close()

  resp, err := FetchHtml("GET", ts.URL, nil)
  if err != nil {
    t.Errorf("Some error occured while sending: %v", err)
  }
  defer resp.Body.Close()

  if resp.StatusCode != http.StatusOK {
    t.Errorf("Status code is %d, expected %d", resp.StatusCode, http.StatusOK)
  }

  dataArr, err := ioutil.ReadAll(resp.Body)
  if err != nil {
    t.Errorf("Some error occured while sending: %v", err)
  }

  data := string(dataArr)
  data = data[:len(data)-1] // trim newline
  if data != expectedData {
    t.Errorf("Body is '%s', expected '%s'", data, expectedData)
  }

  // run without protocol
  _, err = FetchHtml("GET", ts.URL[7:], nil)
  if err != nil {
    t.Errorf("Some error occured while sending: %v", err)
  }
}