github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/Unknwon/com/http_test.go (about) 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 15 package com 16 17 import ( 18 "io/ioutil" 19 "net/http" 20 "strings" 21 "testing" 22 ) 23 24 var examplePrefix = `<!doctype html> 25 <html> 26 <head> 27 <title>Example Domain</title> 28 ` 29 30 func TestHttpGet(t *testing.T) { 31 // 200. 32 rc, err := HttpGet(&http.Client{}, "http://example.com", nil) 33 if err != nil { 34 t.Fatalf("HttpGet:\n Expect => %v\n Got => %s\n", nil, err) 35 } 36 p, err := ioutil.ReadAll(rc) 37 if err != nil { 38 t.Errorf("HttpGet:\n Expect => %v\n Got => %s\n", nil, err) 39 } 40 s := string(p) 41 if !strings.HasPrefix(s, examplePrefix) { 42 t.Errorf("HttpGet:\n Expect => %s\n Got => %s\n", examplePrefix, s) 43 } 44 } 45 46 func TestHttpGetBytes(t *testing.T) { 47 p, err := HttpGetBytes(&http.Client{}, "http://example.com", nil) 48 if err != nil { 49 t.Errorf("HttpGetBytes:\n Expect => %v\n Got => %s\n", nil, err) 50 } 51 s := string(p) 52 if !strings.HasPrefix(s, examplePrefix) { 53 t.Errorf("HttpGet:\n Expect => %s\n Got => %s\n", examplePrefix, s) 54 } 55 } 56 57 func TestHttpGetJSON(t *testing.T) { 58 59 } 60 61 type rawFile struct { 62 name string 63 rawURL string 64 data []byte 65 } 66 67 func (rf *rawFile) Name() string { 68 return rf.name 69 } 70 71 func (rf *rawFile) RawUrl() string { 72 return rf.rawURL 73 } 74 75 func (rf *rawFile) Data() []byte { 76 return rf.data 77 } 78 79 func (rf *rawFile) SetData(p []byte) { 80 rf.data = p 81 } 82 83 func TestFetchFiles(t *testing.T) { 84 files := []RawFile{ 85 &rawFile{rawURL: "http://example.com"}, 86 &rawFile{rawURL: "http://example.com"}, 87 } 88 err := FetchFiles(&http.Client{}, files, nil) 89 if err != nil { 90 t.Errorf("FetchFiles:\n Expect => %v\n Got => %s\n", nil, err) 91 } else if len(files[0].Data()) != 1270 { 92 t.Errorf("FetchFiles:\n Expect => %d\n Got => %d\n", 1270, len(files[0].Data())) 93 } else if len(files[1].Data()) != 1270 { 94 t.Errorf("FetchFiles:\n Expect => %d\n Got => %d\n", 1270, len(files[1].Data())) 95 } 96 } 97 98 func TestFetchFilesCurl(t *testing.T) { 99 files := []RawFile{ 100 &rawFile{rawURL: "http://example.com"}, 101 &rawFile{rawURL: "http://example.com"}, 102 } 103 err := FetchFilesCurl(files) 104 if err != nil { 105 t.Errorf("FetchFilesCurl:\n Expect => %v\n Got => %s\n", nil, err) 106 } else if len(files[0].Data()) != 1270 { 107 t.Errorf("FetchFilesCurl:\n Expect => %d\n Got => %d\n", 1270, len(files[0].Data())) 108 } else if len(files[1].Data()) != 1270 { 109 t.Errorf("FetchFilesCurl:\n Expect => %d\n Got => %d\n", 1270, len(files[1].Data())) 110 } 111 }