本文整理汇总了C#中System.Net.Http.HttpClient.AcceptJson方法的典型用法代码示例。如果您正苦于以下问题:C# HttpClient.AcceptJson方法的具体用法?C# HttpClient.AcceptJson怎么用?C# HttpClient.AcceptJson使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Http.HttpClient
的用法示例。
在下文中一共展示了HttpClient.AcceptJson方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateTestAction
protected async Task<Action> CreateTestAction(string verb = "Created for test goal and activity")
{
var session = await Login();
using (var client = new HttpClient { BaseAddress = new Uri(ServerUrl) })
{
client.AcceptJson().AddSessionHeader(session.Id.ToString());
var action = new Action
{
Verb = verb,
Goal =
new Goal
{
Concern = new ConcernMatrix { Coordinates = new Matrix { X = 1, Y = 1 }, Category = 0 },
RewardResource =
new RewardResourceMatrix { Coordinates = new Matrix { X = 2, Y = 2 }, Category = 0 },
Feedback = new GoalFeedback { Threshold = 0, Target = 0, Direction = 0 },
Description = "Created for test cases"
},
Activity = new Activity { Name = "Testing" }
};
var actionResponse = await client.PostAsJsonAsync("/api/actions", action);
Assert.Equal(HttpStatusCode.Created, actionResponse.StatusCode);
var created = await actionResponse.Content.ReadAsJsonAsync<Action>();
return created;
}
}
示例2: AddActivityGoal
public async Task AddActivityGoal()
{
var session = await Login();
var currentSeed = Guid.NewGuid();
using (var client = new HttpClient { BaseAddress = new Uri(ServerUrl) })
{
client.AcceptJson().AddSessionHeader(session.Id);
var form = new Activity { Name = $"Name.{currentSeed}", Description = $"Description.{currentSeed}" };
// Add Activity with valid data
var activityResponse = await client.PostAsJsonAsync($"/api/activities", form);
Assert.Equal(HttpStatusCode.Created, activityResponse.StatusCode);
var activity = await activityResponse.Content.ReadAsJsonAsync<Activity>();
Assert.Equal($"Name.{currentSeed}", activity.Name);
var goalForm = new ActivityGoalForm { Description = $"Description.{currentSeed}" };
activityResponse = await client.PutAsJsonAsync($"/api/activities/{activity.Id}/goal", goalForm);
Assert.Equal(HttpStatusCode.OK, activityResponse.StatusCode);
activity = await activityResponse.Content.ReadAsJsonAsync<Activity>();
Assert.Equal(1, activity.Goals.Count);
}
}
示例3: WhoAmIWithValidSession
public async Task WhoAmIWithValidSession()
{
var session = await Login();
using (var client = new HttpClient { BaseAddress = new Uri(ServerUrl) })
{
client.AcceptJson().AddSessionHeader(session.Id);
var playerResponse = await client.GetAsync("/api/players");
Assert.Equal(HttpStatusCode.OK, playerResponse.StatusCode);
var player = await playerResponse.Content.ReadAsJsonAsync<Player>();
Assert.Equal(session.Player.Id, player.Id);
}
}
示例4: GetMyGroups_WithValidSession
public async Task GetMyGroups_WithValidSession()
{
var session = await Login();
using (var client = new HttpClient { BaseAddress = new Uri(ServerUrl) })
{
client.AcceptJson().AddSessionHeader(session.Id);
// Get My Groups with valid session header
var groupResponse = await client.GetAsync("/api/groups");
Assert.Equal(HttpStatusCode.OK, groupResponse.StatusCode);
var groups = await groupResponse.Content.ReadAsJsonAsync<IList<Group>>();
Assert.IsType(typeof(List<Group>), groups);
}
}
示例5: GetPlayerWithInvalidSession
public async Task GetPlayerWithInvalidSession()
{
var sessionId = "unknown";
using (var client = new HttpClient { BaseAddress = new Uri(ServerUrl) })
{
client.AcceptJson().AddSessionHeader(sessionId);
// Get Player with invalid session header
var playerResponse = await client.GetAsync("/api/players");
Assert.Equal(HttpStatusCode.Unauthorized, playerResponse.StatusCode);
var fetched = await playerResponse.Content.ReadAsJsonAsync<ApiError>();
Assert.Equal($"Invalid {SessionAuthorizeFilter.SessionHeaderName} Header.", fetched.Error);
}
}
示例6: GetPlayerWithNonExistingSession
public async Task GetPlayerWithNonExistingSession()
{
var sessionId = Guid.NewGuid();
using (var client = new HttpClient { BaseAddress = new Uri(ServerUrl) })
{
client.AcceptJson().AddSessionHeader(sessionId);
// Get Player with non-existing session header
var playerResponse = await client.GetAsync("/api/players");
Assert.Equal(HttpStatusCode.NotFound, playerResponse.StatusCode);
var fetched = await playerResponse.Content.ReadAsJsonAsync<ApiError>();
Assert.Equal($"Session {sessionId} is Invalid.", fetched.Error);
}
}
示例7: GetActorGroups_WithInvalidActor
public async Task GetActorGroups_WithInvalidActor()
{
var session = await Login();
using (var client = new HttpClient { BaseAddress = new Uri(ServerUrl) })
{
client.AcceptJson().AddSessionHeader(session.Id);
// Get Actor's Groups with invalid Actor
var invalidId = Guid.NewGuid();
var groupResponse = await client.GetAsync($"/api/groups/actor/{invalidId}");
Assert.Equal(HttpStatusCode.NotFound, groupResponse.StatusCode);
var fetched = await groupResponse.Content.ReadAsJsonAsync<ApiError>();
Assert.Equal($"No such Player found.", fetched.Error);
}
}
示例8: Login
protected async Task<Session> Login(string username = "mayur", string password = "mayur")
{
using (var client = new HttpClient { BaseAddress = new Uri(ServerUrl) })
{
client.AcceptJson();
// login
var loginForm = new UserForm { Username = username, Password = password };
var loginResponse = await client.PostAsJsonAsync("/api/sessions", loginForm);
Assert.True(loginResponse.IsSuccessStatusCode);
var created = await loginResponse.Content.ReadAsJsonAsync<Session>();
return created;
}
}
示例9: GetInvalidAction
public async Task GetInvalidAction()
{
var session = await Login();
var invalidId = Guid.NewGuid();
using (var client = new HttpClient { BaseAddress = new Uri(ServerUrl) })
{
client.AcceptJson().AddSessionHeader(session.Id.ToString());
// Get action with Invalid Id
var actionResponse = await client.GetAsync($"/api/actions/{invalidId}");
Assert.Equal(HttpStatusCode.NotFound, actionResponse.StatusCode);
var content = await actionResponse.Content.ReadAsJsonAsync<ApiError>();
Assert.Equal($"No Action found.", content.Error);
}
}
示例10: AddActivityGoal_InvalidId
public async Task AddActivityGoal_InvalidId()
{
var session = await Login();
using (var client = new HttpClient { BaseAddress = new Uri(ServerUrl) })
{
client.AcceptJson().AddSessionHeader(session.Id);
var form = new UserForm();
// Add Activity Goal with invalid Id
var invalidId = Guid.NewGuid();
var activityResponse = await client.PutAsJsonAsync($"/api/activities/{invalidId}/goal", form);
Assert.Equal(HttpStatusCode.NotFound, activityResponse.StatusCode);
var content = await activityResponse.Content.ReadAsJsonAsync<ApiError>();
Assert.Equal("No such Activity found.", content.Error);
}
}
示例11: CreateTestGoal
protected async Task<Goal> CreateTestGoal()
{
var session = await Login();
using (var client = new HttpClient { BaseAddress = new Uri(ServerUrl) })
{
client.AcceptJson().AddSessionHeader(session.Id.ToString());
var goal = new Goal
{
Concern = new ConcernMatrix { Coordinates = new Matrix { X = 1, Y = 1 }, Category = 0 },
RewardResource =
new RewardResourceMatrix { Coordinates = new Matrix { X = 2, Y = 2 }, Category = 0 },
Feedback = new GoalFeedback { Threshold = 0, Target = 0, Direction = 0 },
Description = "Created for test cases"
};
var goalResponse = await client.PostAsJsonAsync("/api/goals", goal);
Assert.Equal(HttpStatusCode.Created, goalResponse.StatusCode);
var created = await goalResponse.Content.ReadAsJsonAsync<Goal>();
return created;
}
}
示例12: CreateValidActionRelation
public async Task CreateValidActionRelation()
{
var session = await Login();
using (var client = new HttpClient { BaseAddress = new Uri(ServerUrl) })
{
client.AcceptJson().AddSessionHeader(session.Id.ToString());
var newAction = await CreateTestAction();
var newAR = new ActionRelation
{
ActionId = newAction.Id,
Relationship = 0,
ConcernChange = new Matrix { X = 0, Y = 0 },
RewardResourceChange = new Matrix { X = 0, Y = 0 }
};
var arResponse = await client.PostAsJsonAsync("/api/actions/relations", newAR);
Assert.Equal(HttpStatusCode.Created, arResponse.StatusCode);
var action = await arResponse.Content.ReadAsJsonAsync<ActionRelation>();
Assert.IsType(typeof(ActionRelation), action);
}
}
示例13: UpdateRewardByInvalidAction
public async Task UpdateRewardByInvalidAction()
{
var session = await Login();
using (var client = new HttpClient { BaseAddress = new Uri(ServerUrl) })
{
client.AcceptJson().AddSessionHeader(session.Id.ToString());
var actionForm = new Action { Verb = "invalidVerb" };
var actionResponse = await client.PostAsJsonAsync("/api/actions/send", actionForm);
Assert.Equal(HttpStatusCode.NotFound, actionResponse.StatusCode);
var content = await actionResponse.Content.ReadAsJsonAsync<ApiError>();
Assert.Equal($"Invalid action verb.", content.Error);
}
}
示例14: CreateActionWithInvalidGoalId
public async Task CreateActionWithInvalidGoalId()
{
var session = await Login();
using (var client = new HttpClient { BaseAddress = new Uri(ServerUrl) })
{
client.AcceptJson().AddSessionHeader(session.Id.ToString());
var newAction = await CreateTestAction();
var actionForm = new Action
{
Verb = "testerVerb",
ActivityId = newAction.ActivityId,
GoalId = new Guid()
};
var actionResponse = await client.PostAsJsonAsync("/api/actions", actionForm);
Assert.Equal(HttpStatusCode.NotFound, actionResponse.StatusCode);
var content = await actionResponse.Content.ReadAsJsonAsync<ApiError>();
Assert.Equal($"No Goal found", content.Error);
}
}
示例15: CreateActionRelationWithInvalidActionId
public async Task CreateActionRelationWithInvalidActionId()
{
var session = await Login();
using (var client = new HttpClient { BaseAddress = new Uri(ServerUrl) })
{
client.AcceptJson().AddSessionHeader(session.Id.ToString());
var newAR = new ActionRelation
{
ActionId = new Guid(),
Relationship = 0,
ConcernChange = new Matrix { X = 0, Y = 0 },
RewardResourceChange = new Matrix { X = 0, Y = 0 }
};
var arResponse = await client.PostAsJsonAsync("/api/actions/relations", newAR);
Assert.Equal(HttpStatusCode.NotFound, arResponse.StatusCode);
var content = await arResponse.Content.ReadAsJsonAsync<ApiError>();
Assert.Equal($"No Action found", content.Error);
}
}