public ScorePost makeScorePost(int score) { ScorePost post = new ScorePost(); post.player = playerName; post.score = score; post.time = System.DateTime.Now.ToString("o"); string toValidate = $"{gameId}-{score}-{playerName}-{post.time}-{secret}"; Debug.Log(toValidate); var encoding = new System.Text.UTF8Encoding(); byte[] validateBytes = encoding.GetBytes(toValidate); byte[] hash = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(validateBytes); post.validation = System.BitConverter.ToString(hash).Replace("-", string.Empty).ToLower(); return(post); }
public async Task Post_GivenValidScorePost_CallsPublishWithScoreViewAndReturnsCreated() { var score = new ScorePost() { Name = "Test Name", Score = 10, }; var gameId = Guid.NewGuid(); _publisherMock.Setup(p => p.Publish(It.IsAny <ScoreView>())).ReturnsAsync(true); var result = await _controller.Post(gameId, score); var created = result as CreatedResult; Assert.That(created, Is.Not.Null); Assert.That(created.StatusCode, Is.EqualTo((int)HttpStatusCode.Created)); Assert.That(created.Value, Is.TypeOf <Guid>()); _publisherMock.Verify(p => p.Publish(It.IsAny <ScoreView>()), Times.Once, "Publish called incorrectly"); }
public async Task Post_GivenPublisherThrowingException_ReturnsInternalServerErrorWithMessage() { var expectedMessage = "Something went wrong while submitting score."; _publisherMock.Setup(p => p.Publish(It.IsAny <ScoreView>())).ThrowsAsync(new Exception(expectedMessage)); var score = new ScorePost() { Name = "Test Name", Score = 10, }; var gameId = Guid.NewGuid(); var result = await _controller.Post(gameId, score); var error = result as ObjectResult; Assert.That(error.StatusCode, Is.EqualTo((int)HttpStatusCode.InternalServerError)); Assert.That(error.Value.ToString(), Is.EqualTo(expectedMessage)); }
IEnumerator PostScore(ScorePost score) { WWWForm form = new WWWForm(); form.AddField("player", score.player); form.AddField("score", score.score); form.AddField("time", score.time); form.AddField("validation", score.validation); UnityWebRequest req = UnityWebRequest.Post(baseUrl + "/game/" + gameId + "/score", form); yield return(req.SendWebRequest()); if (req.result != UnityWebRequest.Result.Success) { Debug.Log(req.error); } RefreshScores(); }
IEnumerator SubmitScore(string name, int score) { var s = new ScorePost(); s.score = score; s.user = name; s.game = gameId; Debug.Log(JsonUtility.ToJson(s)); UnityWebRequest www = UnityWebRequest.Put(host + "submit/", JsonUtility.ToJson(s)); www.SetRequestHeader("Content-Type", "application/json"); www.method = "POST"; yield return(www.SendWebRequest()); if (www.isNetworkError || www.isHttpError) { Debug.Log(www.error); } else { Debug.Log("Form upload complete!"); } }