mock是个好东东,

在大项目或大公司,很实用,

因为很多环境不是随时在开发环境可得的。

package main

import (
	"testing"
	"net/http"
	"fmt"
	"net/http/httptest"
)

const checkMark = " OK! "
const ballotX = " ERROR! "

var feed = `<?xml version="1.0" encoding="UTF-8"?>
		<rss>
			<channel>
				<title>Going Go Programming</title>
				<description>Golang : https://github.com/goinggo</description>
				<link>http://www.goinggo.net/</link>
				<item>
					<pubDate>Sun, 15 Mar 2015 15:04:00 +0000</pubDate>
					<title>Object Oriented Programming Mechanics</title>
					<description>Go is an object oriented language.</description>
					<link>http://www.goinggo.net/2015/03/object-oriented</link>
				</item>
		</channel>
	</rss>`

func mockServer() *httptest.Server {
	f := func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(200)
		w.Header().Set("Content-Type", "application/xml")
		fmt.Fprintln(w, feed)
	}
	return httptest.NewServer(http.HandlerFunc(f))
}

func TestDownload(t *testing.T) {
	statusCode := http.StatusOK

	server := mockServer()
	defer server.Close()

	t.Log("Given the need to test downloading content.")
	{

		t.Logf("\tWhen checking \"%s\" for status code \"%d\"", server.URL, statusCode)
		{
			resp, err := http.Get(server.URL)
			if err != nil {
				t.Fatal("\tShould be able to make the Get call.", ballotX, err)
			}
			t.Log("\t\tShould be able to make the Get call.", checkMark)
			defer resp.Body.Close()

			if resp.StatusCode == statusCode {
				t.Logf("\t\tShould receive a \"%d\" status, %v", statusCode, checkMark)
			} else {
				t.Errorf("\t\tShould receive a \"%d\" status. %v %v", statusCode, ballotX, resp.StatusCode)
			}
		}
	}
}

  

最新文章

  1. not in 和 not EXISTS 的区别
  2. Redis 主从配置和参数详解
  3. NodeJS中 package.json 解析
  4. MapReduce: 一种简化的大规模集群数据处理法
  5. Python -- BeautifulSoup的学习使用
  6. 检索 COM 类工厂中 CLSID 为 {} 的组件时失败,原因是出现以下错误: 80070005
  7. linux 下使用crontab 定时打包日志并删除已被打包的日志
  8. [翻译][MVC 5 + EF 6] 6:创建更复杂的数据模型
  9. Dict和Set类型
  10. ReentrantLock(重入锁)以及公平性
  11. css学习笔记三
  12. 树&amp;堆
  13. Shpinx在PHPCMS里的使用及配置
  14. 我是如何用redis做实时订阅推送的
  15. 学习笔记--python中使用多进程、多线程加速文本预处理
  16. 鱼骨时间轴案例(转自CSDN,原文链接附于文中)
  17. Google开源软负载seesaw
  18. Cpython支持的进程与线程
  19. ux.form.field.Month 只能选年、月的时间扩展
  20. Http协议基础及发展历史

热门文章

  1. IE下textarea去除回车换行符
  2. RobHess的SIFT源码分析:imgfeatures.h和imgfeatures.c文件
  3. Ubuntu 16.04安装NVIDIA驱动后循环登录问题
  4. [zabbix]zabbix分区表操作步骤
  5. Iterator与ListIterator的区别
  6. String作为输出型参数时获取不到值
  7. 数据结构:K-D树
  8. redis启动脚本
  9. 【洛谷 P4180】【模板】严格次小生成树[BJWC2010](倍增)
  10. Android控件——ImageView