当前位置: 首页>>代码示例>>Golang>>正文


Golang aetest.NewInstance函数代码示例

本文整理汇总了Golang中google/golang.org/appengine/aetest.NewInstance函数的典型用法代码示例。如果您正苦于以下问题:Golang NewInstance函数的具体用法?Golang NewInstance怎么用?Golang NewInstance使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了NewInstance函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TestIndexHandler

func TestIndexHandler(t *testing.T) {
	_, done, err := aetest.NewContext()
	if err != nil {
		t.Fatal(err)
	}
	defer done()

	inst, err := aetest.NewInstance(nil)
	if err != nil {
		t.Errorf("Failed to create instance: %v", err)
	}
	defer inst.Close()

	request, err := inst.NewRequest("GET", "/", nil)
	if err != nil {
		t.Errorf("Error creating new instance %v", err)
	}

	response := httptest.NewRecorder()
	IndexHandler(response, request)
	if response.Code != http.StatusFound {
		t.Errorf("Did not get StatusFound(302) got %v", response.Code)
	}
	if response.Header().Get("Location") != "/index.html" {
		t.Errorf("Expected Location: /index.html but instead got %v", response.Header().Get("Location"))
	}
}
开发者ID:dherbst,项目名称:go-appengine-example,代码行数:27,代码来源:handlers_test.go

示例2: TestServerTime

// TestServerTime calls the GetTime method of the messaging to test the time
func TestServerTime(t *testing.T) {
	/*context, err := aetest.NewContext(nil)
	if err != nil {
		t.Fatal(err)
	}
	defer context.Close()*/
	inst, err := aetest.NewInstance(&aetest.Options{"", true})
	context := CreateContext(inst)

	if err != nil {
		t.Fatal(err)
	}
	defer inst.Close()

	uuid := ""
	w, req := InitAppEngineContext(t)

	pubnubInstance := messaging.New(context, uuid, w, req, PubKey, SubKey, "", "", false)
	//pubnubInstance := messaging.NewPubnub(PubKey, SubKey, "", "", false, "")

	returnTimeChannel := make(chan []byte)
	errorChannel := make(chan []byte)
	responseChannel := make(chan string)
	waitChannel := make(chan string)

	//go pubnubInstance.GetTime(returnTimeChannel, errorChannel)
	go pubnubInstance.GetTime(context, w, req, returnTimeChannel, errorChannel)
	go ParseTimeResponse(returnTimeChannel, responseChannel)
	go ParseErrorResponse(errorChannel, responseChannel)
	go WaitForCompletion(responseChannel, waitChannel)
	ParseWaitResponse(waitChannel, t, "Time")
}
开发者ID:pubnub,项目名称:go,代码行数:33,代码来源:pubnubTime_test.go

示例3: TestSuccessCodeAndInfo

// TestSuccessCodeAndInfo sends out a message to the pubnub channel
// The response is parsed and should match the 'sent' status.
// _publishSuccessMessage is defined in the common.go file
func TestSuccessCodeAndInfo(t *testing.T) {
	/*context, err := aetest.NewContext(nil)
	if err != nil {
		t.Fatal(err)
	}
	defer context.Close()*/
	inst, err := aetest.NewInstance(&aetest.Options{"", true})
	context := CreateContext(inst)

	if err != nil {
		t.Fatal(err)
	}
	defer inst.Close()

	uuid := ""
	w, req := InitAppEngineContext(t)

	pubnubInstance := messaging.New(context, uuid, w, req, PubKey, SubKey, "", "", false)
	//pubnubInstance := messaging.NewPubnub(PubKey, SubKey, "", "", false, "")
	channel := "testChannel"
	message := "Pubnub API Usage Example"
	returnChannel := make(chan []byte)
	errorChannel := make(chan []byte)
	responseChannel := make(chan string)
	waitChannel := make(chan string)

	//go pubnubInstance.Publish(channel, message, returnChannel, errorChannel)
	go pubnubInstance.Publish(context, w, req, channel, message, returnChannel, errorChannel)
	go ParsePublishResponse(returnChannel, channel, publishSuccessMessage, "SuccessCodeAndInfo", responseChannel)

	go ParseErrorResponse(errorChannel, responseChannel)
	go WaitForCompletion(responseChannel, waitChannel)
	ParseWaitResponse(waitChannel, t, "SuccessCodeAndInfo")
	time.Sleep(2 * time.Second)
}
开发者ID:pubnub,项目名称:go,代码行数:38,代码来源:pubnubPublish_test.go

示例4: TestRedirect

func TestRedirect(t *testing.T) {
	ti, err := aetest.NewInstance(nil)
	if err != nil {
		t.Fatal(err)
	}
	defer ti.Close()

	const (
		redirectTo = "https://www.example.com"
		code       = http.StatusFound
	)
	handler := redirectHandler(redirectTo, code)
	urls := []string{"/", "/page", "/page/", "/page?with=query"}
	for _, u := range urls {
		req, err := ti.NewRequest("GET", u, nil)
		if err != nil {
			t.Errorf("%s: %v", u, err)
			continue
		}
		req.Host = "example.org"
		res := httptest.NewRecorder()
		handler.ServeHTTP(res, req)
		if res.Code != code {
			t.Errorf("%s: res.Code = %d; want %d", u, res.Code, code)
		}
		redir := redirectTo + u
		if v := res.Header().Get("location"); v != redir {
			t.Errorf("%s: location = %q; want %q", u, v, redir)
		}
	}
}
开发者ID:kleopatra999,项目名称:weasel,代码行数:31,代码来源:handle_test.go

示例5: TestGetSpot

func TestGetSpot(t *testing.T) {
	opt := aetest.Options{AppID: "t2jp-2015", StronglyConsistentDatastore: true}
	inst, err := aetest.NewInstance(&opt)
	defer inst.Close()
	input, err := json.Marshal(Spot{SpotName: "foo", Body: "bar"})
	req, err := inst.NewRequest("POST", "/edit/v1/spots", bytes.NewBuffer(input))
	if err != nil {
		t.Fatalf("Failed to create req: %v", err)
	}
	loginUser := user.User{Email: "[email protected]", Admin: false, ID: "111111"}
	aetest.Login(&loginUser, req)
	// ctx := appengine.NewContext(req)
	res := httptest.NewRecorder()
	c := web.C{}
	spotCreateHandler(c, res, req)
	if res.Code != http.StatusCreated {
		t.Fatalf("Fail to request spots create, status code: %v", res.Code)
	}
	var getResponse GetResponse
	err = json.NewDecoder(res.Body).Decode(&getResponse)
	spotCode := getResponse.Item.SpotCode
	t.Logf("spot code: %v", strconv.FormatInt(spotCode, 10))
	getReq, err := inst.NewRequest("GET", "/edit/v1/spots/"+strconv.FormatInt(spotCode, 10), nil)
	if err != nil {
		t.Fatalf("Failed to create req: %v", err)
	}
	getRes := httptest.NewRecorder()
	getC := web.C{URLParams: map[string]string{"spotCode": strconv.FormatInt(spotCode, 10)}}
	spotGetHandler(getC, getRes, getReq)
	if getRes.Code != http.StatusOK {
		t.Fatalf("Fail to request spot get, status code: %v", getRes.Code)
	}
}
开发者ID:yosukesuzuki,项目名称:t2tapp,代码行数:33,代码来源:main_test.go

示例6: TestIndexPage

func TestIndexPage(t *testing.T) {
	inst, err := aetest.NewInstance(nil)
	if err != nil {
		t.Fatalf("Failed to create instance: %v", err)
	}
	defer inst.Close()
	req, err := inst.NewRequest("GET", "/", nil)
	if err != nil {
		t.Fatalf("Failed to create request: %v", err)
	}
	w := httptest.NewRecorder()
	var vars map[string]string
	var settings Settings
	file, err := ioutil.ReadFile("./settings_test.json")
	if err == nil {
		json.Unmarshal(file, &settings)
	}
	ctx, _, _ := aetest.NewContext()
	index(&Context{
		AE:       ctx,
		Request:  req,
		Response: w,
		Settings: &settings,
		Vars:     vars,
	})
	if w.Code != 200 {
		t.Errorf("Index did not return 200. Returned type was %d", w.Code)
	}
}
开发者ID:rctl,项目名称:rasmusj.se,代码行数:29,代码来源:index_test.go

示例7: TestApi1ExpoCurrentHandlerNoExpo

func TestApi1ExpoCurrentHandlerNoExpo(t *testing.T) {
	inst, err := aetest.NewInstance(nil)
	if err != nil {
		t.Errorf("Failed to create instance: %v", err)
	}
	defer inst.Close()

	request, err := inst.NewRequest("GET", "/api/1/expo/current", nil)
	if err != nil {
		t.Errorf("Error creating new instance %v", err)
	}

	response := httptest.NewRecorder()
	Api1ExpoCurrentHandler(response, request)
	if response.Code != http.StatusOK {
		t.Errorf("Did not get StatusOK got %v", response.Code)
	}
	data, err := ioutil.ReadAll(response.Body)
	if err != nil {
		t.Errorf("Error reading body %v", err)
	}
	expected := "{ \"NumTeams\": 0, \"NumRegistered\": 0 }"
	if string(data) != expected {
		t.Errorf("Expected  %s got %v", expected, string(data))
	}
}
开发者ID:dherbst,项目名称:lmjrfll,代码行数:26,代码来源:api_test.go

示例8: TestApi1UserProfileHandler

func TestApi1UserProfileHandler(t *testing.T) {
	inst, err := aetest.NewInstance(nil)
	if err != nil {
		t.Errorf("Failed to create instance: %v", err)
	}
	defer inst.Close()

	request, err := inst.NewRequest("GET", "/api/1/user/profile", nil)
	if err != nil {
		t.Errorf("Error creating new instance %v", err)
	}

	response := httptest.NewRecorder()
	Api1UserProfileHandler(response, request)
	if response.Code != http.StatusOK {
		t.Errorf("Did not get StatusOK got %v", response.Code)
	}

	data, err := ioutil.ReadAll(response.Body)
	if err != nil {
		t.Errorf("Error reading body %v", err)
	}
	prefix := `{"Name":"","Email":"","LogoutUrl":"","LoginUrl":"/_ah/login?continue=`
	if !strings.HasPrefix(string(data), prefix) {
		t.Errorf("Expected  %s got %v", prefix, string(data))
	}

}
开发者ID:dherbst,项目名称:lmjrfll,代码行数:28,代码来源:api_test.go

示例9: TestHook

func TestHook(t *testing.T) {
	ti, err := aetest.NewInstance(nil)
	if err != nil {
		t.Fatal(err)
	}
	defer ti.Close()

	body := `{"bucket": "dummy", "name": "path/obj"}`
	req, _ := ti.NewRequest("POST", "/-/hook/gcs", strings.NewReader(body))

	const cacheKey = "dummy/path/obj"
	ctx := appengine.NewContext(req)
	item := &memcache.Item{Key: cacheKey, Value: []byte("ignored")}
	if err := memcache.Set(ctx, item); err != nil {
		t.Fatal(err)
	}

	// must remove cached item
	res := httptest.NewRecorder()
	http.DefaultServeMux.ServeHTTP(res, req)
	if res.Code != http.StatusOK {
		t.Errorf("res.Code = %d; want %d", res.Code, http.StatusOK)
	}
	if _, err := memcache.Get(ctx, cacheKey); err != memcache.ErrCacheMiss {
		t.Fatalf("memcache.Get(%q): %v; want ErrCacheMiss", cacheKey, err)
	}

	// cache misses must not respond with an error code
	req, _ = ti.NewRequest("POST", "/-/hook/gcs", strings.NewReader(body))
	res = httptest.NewRecorder()
	http.DefaultServeMux.ServeHTTP(res, req)
	if res.Code != http.StatusOK {
		t.Errorf("res.Code = %d; want %d", res.Code, http.StatusOK)
	}
}
开发者ID:kleopatra999,项目名称:weasel,代码行数:35,代码来源:handle_test.go

示例10: TestAddLocationTestify

func TestAddLocationTestify(t *testing.T) {
	inst, err := aetest.NewInstance(
		&aetest.Options{StronglyConsistentDatastore: true})
	require.Nil(t, err, "Error creating aetest instance: %v", err)
	defer inst.Close()
	rt := initRouter()

	loc, err := json.Marshal(&Location{
		Name: "Cambridge Fresh Pond",
		Lat:  42.5,
		Lng:  -71.5,
	})
	assert.Nil(t, err, "Error marshalling Location into JSON: %v", err)

	req, err := inst.NewRequest("POST", "/add-location",
		ioutil.NopCloser(bytes.NewBuffer(loc)))
	require.Nil(t, err, "Error preparing request: %v", err)
	rec := httptest.NewRecorder()
	rt.ServeHTTP(rec, req)

	const expectedResponse = `{"addLocation":"success"}`
	assert.Equal(t, expectedResponse, string(rec.Body.Bytes()),
		"Expected response to be %s, got %s", expectedResponse, string(rec.Body.Bytes()))

	dbReq, err := inst.NewRequest("GET", "/", nil)
	require.Nil(t, err, "Error preparing request: %v", err)
	ctx := appengine.NewContext(dbReq)

	q := datastore.NewQuery("Location")
	numLocs, err := q.Count(ctx)
	require.Nil(t, err, "Error preparing request: %v", err)
	assert.Equal(t, 1, numLocs, "Expected number of locations to be 1, got %d", numLocs)
}
开发者ID:AndyHaskell,项目名称:testify-tutorial,代码行数:33,代码来源:api_test.go

示例11: SetupSuite

func (s *TestSuite) SetupSuite() {
	var err error
	s.inst, err = aetest.NewInstance(
		&aetest.Options{StronglyConsistentDatastore: true})
	require.Nil(s.T(), err, "Error creating aetest instance: %v", err)
	s.rt = initRouter()
}
开发者ID:AndyHaskell,项目名称:testify-tutorial,代码行数:7,代码来源:api_test.go

示例12: setUp

func setUp() {
	server = NewServer()
	opt := aetest.Options{AppID: "auth", StronglyConsistentDatastore: true}
	var err error
	inst, err = aetest.NewInstance(&opt)
	if err != nil || inst == nil {
		log.Printf("ERROR: %v", fmt.Errorf("Failed to created aetest instance, %v", err))
		inst.Close()
	}
	r, err := inst.NewRequest("GET", "/", nil)
	if err != nil {
		log.Panicf("Failed to create request: %v", err)
		inst.Close()
	}
	c := appengine.NewContext(r)

	_, err = setupFakeClient(c)
	if err != nil {
		log.Panicf("Failed to create fake client: %v", err)
		inst.Close()
	}

	userKey, err := setupFakeUser(c)
	if err != nil {
		log.Panicf("Failed to create fake user: %v", err)
		inst.Close()
	}

	_, err = setupFakeAccessToken(c, userKey)
	if err != nil {
		log.Panicf("Failed to create fake access token: %v", err)
		inst.Close()
	}
}
开发者ID:credli,项目名称:oauth2provider,代码行数:34,代码来源:oauth2provider_test.go

示例13: TestMain

func TestMain(m *testing.M) {
	oauth1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(`{"access_token": "oauth1-token"}`))
	}))

	config.Dir = "app"
	config.Env = "dev"
	config.Prefix = "/myprefix"
	config.Google.Auth.Client = "test-client-id"
	config.Google.ServiceAccount.Key = ""
	config.Twitter.TokenURL = oauth1.URL + "/"
	config.SyncToken = "sync-token"
	config.Schedule.Start = time.Date(2015, 5, 28, 9, 0, 0, 0, time.UTC)
	config.Schedule.Timezone = "America/Los_Angeles"
	var err error
	config.Schedule.Location, err = time.LoadLocation(config.Schedule.Timezone)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Could not load location %q", config.Schedule.Location)
		os.Exit(1)
	}

	// a test instance shared across multiple tests
	if aetestInstance, err = aetest.NewInstance(nil); err != nil {
		panic(fmt.Sprintf("aetestInstance: %v", err))
	}

	// run all tests
	code := m.Run()
	// cleanup
	oauth1.Close()
	aetestInstance.Close()
	cleanupTests()

	os.Exit(code)
}
开发者ID:CadeLaRen,项目名称:ioweb2016,代码行数:35,代码来源:main_test.go

示例14: TestCreateSpot

func TestCreateSpot(t *testing.T) {
	opt := aetest.Options{AppID: "t2jp-2015", StronglyConsistentDatastore: true}
	inst, err := aetest.NewInstance(&opt)
	defer inst.Close()
	input, err := json.Marshal(Spot{SpotName: "foo", Body: "bar"})
	req, err := inst.NewRequest("POST", "/edit/v1/spots", bytes.NewBuffer(input))
	if err != nil {
		t.Fatalf("Failed to create req: %v", err)
	}
	loginUser := user.User{Email: "[email protected]", Admin: false, ID: "111111"}
	aetest.Login(&loginUser, req)
	ctx := appengine.NewContext(req)
	res := httptest.NewRecorder()
	c := web.C{}
	spotCreateHandler(c, res, req)
	if res.Code != http.StatusCreated {
		t.Fatalf("Fail to request spots create, status code: %v", res.Code)
	}
	spots := []Spot{}
	_, err = datastore.NewQuery("Spot").Order("-UpdatedAt").GetAll(ctx, &spots)
	for i := 0; i < len(spots); i++ {
		t.Logf("SpotCode:%v", spots[i].SpotCode)
		t.Logf("SpotName:%v", spots[i].SpotName)
	}
	if spots[0].SpotName != "foo" {
		t.Fatalf("not expected value! :%v", spots[0].SpotName)
	}

}
开发者ID:yosukesuzuki,项目名称:t2tapp,代码行数:29,代码来源:main_test.go

示例15: TestFlashHeader

func TestFlashHeader(t *testing.T) {
	// create fake GET request
	inst, err := aetest.NewInstance(nil)
	if err != nil {
		t.Fatalf("Failed to create instance: %v", err)
	}
	defer inst.Close()

	r, _ := inst.NewRequest("GET", "/", nil)
	w := httptest.NewRecorder()

	// setup the handler
	handler := NewControllerRegister()
	handler.Add("/", &TestFlashController{}, "get:TestWriteFlash")
	handler.ServeHTTP(w, r)

	// get the Set-Cookie value
	sc := w.Header().Get("Set-Cookie")
	// match for the expected header
	res := strings.Contains(sc, "BEEGO_FLASH=%00notice%23BEEGOFLASH%23TestFlashString%00")
	// validate the assertion
	if res != true {
		t.Errorf("TestFlashHeader() unable to validate flash message")
	}
}
开发者ID:lamproae,项目名称:beegae,代码行数:25,代码来源:flash_test.go


注:本文中的google/golang.org/appengine/aetest.NewInstance函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。