http_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2013 com authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // 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, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package com
  15. import (
  16. "io/ioutil"
  17. "net/http"
  18. "strings"
  19. "testing"
  20. )
  21. var examplePrefix = `<!doctype html>
  22. <html>
  23. <head>
  24. <title>Example Domain</title>
  25. `
  26. func TestHttpGet(t *testing.T) {
  27. // 200.
  28. rc, err := HttpGet(&http.Client{}, "http://example.com", nil)
  29. if err != nil {
  30. t.Fatalf("HttpGet:\n Expect => %v\n Got => %s\n", nil, err)
  31. }
  32. p, err := ioutil.ReadAll(rc)
  33. if err != nil {
  34. t.Errorf("HttpGet:\n Expect => %v\n Got => %s\n", nil, err)
  35. }
  36. s := string(p)
  37. if !strings.HasPrefix(s, examplePrefix) {
  38. t.Errorf("HttpGet:\n Expect => %s\n Got => %s\n", examplePrefix, s)
  39. }
  40. }
  41. func TestHttpGetBytes(t *testing.T) {
  42. p, err := HttpGetBytes(&http.Client{}, "http://example.com", nil)
  43. if err != nil {
  44. t.Errorf("HttpGetBytes:\n Expect => %v\n Got => %s\n", nil, err)
  45. }
  46. s := string(p)
  47. if !strings.HasPrefix(s, examplePrefix) {
  48. t.Errorf("HttpGet:\n Expect => %s\n Got => %s\n", examplePrefix, s)
  49. }
  50. }
  51. func TestHttpGetJSON(t *testing.T) {
  52. }
  53. type rawFile struct {
  54. name string
  55. rawURL string
  56. data []byte
  57. }
  58. func (rf *rawFile) Name() string {
  59. return rf.name
  60. }
  61. func (rf *rawFile) RawUrl() string {
  62. return rf.rawURL
  63. }
  64. func (rf *rawFile) Data() []byte {
  65. return rf.data
  66. }
  67. func (rf *rawFile) SetData(p []byte) {
  68. rf.data = p
  69. }
  70. func TestFetchFiles(t *testing.T) {
  71. files := []RawFile{
  72. &rawFile{rawURL: "http://example.com"},
  73. &rawFile{rawURL: "http://example.com"},
  74. }
  75. err := FetchFiles(&http.Client{}, files, nil)
  76. if err != nil {
  77. t.Errorf("FetchFiles:\n Expect => %v\n Got => %s\n", nil, err)
  78. } else if len(files[0].Data()) != 1270 {
  79. t.Errorf("FetchFiles:\n Expect => %d\n Got => %d\n", 1270, len(files[0].Data()))
  80. } else if len(files[1].Data()) != 1270 {
  81. t.Errorf("FetchFiles:\n Expect => %d\n Got => %d\n", 1270, len(files[1].Data()))
  82. }
  83. }
  84. func TestFetchFilesCurl(t *testing.T) {
  85. files := []RawFile{
  86. &rawFile{rawURL: "http://example.com"},
  87. &rawFile{rawURL: "http://example.com"},
  88. }
  89. err := FetchFilesCurl(files)
  90. if err != nil {
  91. t.Errorf("FetchFilesCurl:\n Expect => %v\n Got => %s\n", nil, err)
  92. } else if len(files[0].Data()) != 1270 {
  93. t.Errorf("FetchFilesCurl:\n Expect => %d\n Got => %d\n", 1270, len(files[0].Data()))
  94. } else if len(files[1].Data()) != 1270 {
  95. t.Errorf("FetchFilesCurl:\n Expect => %d\n Got => %d\n", 1270, len(files[1].Data()))
  96. }
  97. }