Skip to content
This repository has been archived by the owner on Apr 1, 2024. It is now read-only.

Commit

Permalink
Update for pull request #71
Browse files Browse the repository at this point in the history
  • Loading branch information
vfa-thanhnh committed Oct 12, 2017
1 parent 3bece7d commit 255e201
Show file tree
Hide file tree
Showing 10 changed files with 604 additions and 107 deletions.
12 changes: 6 additions & 6 deletions .travis.yml
@@ -1,12 +1,12 @@
language: csharp
os: osx
before_install:
- chmod +x ./Scripts/install.sh
- chmod +x ./TravisScripts/install.sh
install:
- ./Scripts/install.sh
- ./TravisScripts/install.sh
before_script:
- chmod +x ./Scripts/build.sh
- chmod +rw ./Scripts/unity_build.log
- chmod +rw ./Scripts/test_runner_result.xml
- chmod +x ./TravisScripts/build.sh
- chmod +rw ./TravisScripts/unity_build.log
- chmod +rw ./TravisScripts/test_runner_result.xml
script:
- ./Scripts/build.sh
- ./TravisScripts/build.sh
2 changes: 1 addition & 1 deletion Readme.md
Expand Up @@ -36,7 +36,7 @@

## ライセンス

このSDKのライセンスはApache License Version 2.0、YamlDotNet(https://github.com/aaubry/YamlDotNet)に従います。
このSDKのライセンスはApache License Version 2.0、YamlDotNet(https://github.com/aaubry/YamlDotNet) に従います。

## 参考URL集

Expand Down
5 changes: 0 additions & 5 deletions Scripts/install.sh

This file was deleted.

8 changes: 4 additions & 4 deletions Scripts/build.sh → TravisScripts/build.sh
@@ -1,7 +1,7 @@
# Set project path
project_path=$(pwd)/ncmb_unity
# Log file path
log_file=$(pwd)/Scripts/unity_build.log
log_file=$(pwd)/TravisScripts/unity_build.log

error_code=0

Expand All @@ -20,10 +20,10 @@ else
echo "* Building Mac OS failed. Exited with $?."
error_code=1
fi
# Show log
#cat $log_file
echo "* Unity build log"
cat $log_file

test_result_file=$(pwd)/Scripts/test_runner_result.xml
test_result_file=$(pwd)/TravisScripts/test_runner_result.xml
echo "* Execute Test Runner"
/Applications/Unity/Unity.app/Contents/MacOS/Unity \
-runTests \
Expand Down
5 changes: 5 additions & 0 deletions TravisScripts/install.sh
@@ -0,0 +1,5 @@
# This link changes from time to time.
echo 'Downloading from https://netstorage.unity3d.com/unity/cc85bf6a8a04/MacEditorInstaller/Unity-2017.1.2f1.pkg: '
curl -o Unity.pkg https://netstorage.unity3d.com/unity/cc85bf6a8a04/MacEditorInstaller/Unity-2017.1.2f1.pkg
echo 'Installing Unity.pkg'
sudo installer -dumplog -package Unity.pkg -target /
File renamed without changes.
File renamed without changes.
142 changes: 77 additions & 65 deletions ncmb_unity/Assets/Editor/MockServer.cs
Expand Up @@ -9,30 +9,32 @@ public class MockServer
{
public static String SERVER = NCMBTestSettings.DOMAIN_URL + "/";
private HttpListener listener = null;
private static MockServer instance = null;
private static MockServer instance = null;
//Dictionary to store all mock data
Dictionary<string, List<MockServerObject>> mockObjectDic = new Dictionary<string, List<MockServerObject>>();

public MockServer()
{
listener = new HttpListener();
listener = new HttpListener();
//Add allowed prefixes for listener
listener.Prefixes.Add(SERVER);
listener.Prefixes.Add(SERVER);
listener.Prefixes.Add(SERVER + "2013-09-01/users/");
listener.Prefixes.Add(SERVER + "2013-09-01/file/");
listener.Prefixes.Add(SERVER + "2015-09-01/script/");
listener.Prefixes.Add(SERVER + "2015-09-01/script/");
listener.Prefixes.Add(SERVER + "2013-09-01/classes/");
listener.Prefixes.Add(SERVER + "2013-09-01/installation/");
listener.Start();
listener.Start();
instance = this;
//Call to listen request
WaitForNewRequest();
}

public static void startMock(){
if(instance == null){
public static void startMock()
{
if (instance == null)
{
instance = new MockServer();
instance.ReadMockData();
instance.ReadMockData();
}
}

Expand Down Expand Up @@ -62,55 +64,60 @@ private void checkAndResponse(HttpListenerRequest request, HttpListenerResponse
NCMBSettings._responseValidationFlag = false;
MockServerObject mockObj = null;

StreamReader stream = new StreamReader(request.InputStream);
string bodyJson = stream.ReadToEnd();
if(request.HttpMethod.Equals("GET") && request.Url.ToString().Equals(SERVER)){
StreamReader stream = new StreamReader(request.InputStream);
string bodyJson = stream.ReadToEnd();
if (request.HttpMethod.Equals("GET") && request.Url.ToString().Equals(SERVER))
{
mockObj = new MockServerObject();
mockObj.status = 200;

} else {
foreach (MockServerObject mock in mockObjectDic[request.HttpMethod]){
if (request.Url.ToString().Equals(mock.url))
{
if (bodyJson.Length > 0)
{

}
else
{
foreach (MockServerObject mock in mockObjectDic[request.HttpMethod])
{
if (request.Url.ToString().Equals(mock.url))
{
if (bodyJson.Length > 0)
{
if (bodyJson.Equals(mock.body) || request.ContentType.Equals("multipart/form-data; boundary=_NCMBBoundary"))
{
{
mockObj = mock;
mockObj.request = request;
mockObj.validate();
if (mockObj.status == 200 || mockObj.status == 201)
{
break;
}
}
}
else
{
mockObj.request = request;
mockObj.validate();
if (mockObj.status == 200 || mockObj.status == 201)
{
break;
}
}
}
else
{
mockObj = mock;
mockObj.request = request;
mockObj.validate();
if (mockObj.status == 200 || mockObj.status == 201)
{
break;
}
}
}
mockObj.request = request;
mockObj.validate();
if (mockObj.status == 200 || mockObj.status == 201)
{
break;
}
}
}
}
}

if (mockObj == null){
if (mockObj == null)
{
mockObj = new MockServerObject();
}

response.StatusCode = mockObj.status;
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(mockObj.responseJson);
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
// You must close the output stream.
response.Close();
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
// You must close the output stream.
response.Close();
}

private void ReadMockData()
Expand Down Expand Up @@ -143,37 +150,42 @@ private void ReadMockData()
if (request.Children.Keys.Contains(new YamlScalarNode("body")))
{
var body = request.Children[new YamlScalarNode("body")];
mock.body = body.ToJson().Replace("\"[","[").Replace("]\"", "]");
mock.body = body.ToJson().Replace("\"[", "[").Replace("]\"", "]");
}

if (request.Children.Keys.Contains(new YamlScalarNode("query")))
{
var query = request.Children[new YamlScalarNode("query")];
var query = request.Children[new YamlScalarNode("query")];
String queryString = "";

if(query is YamlMappingNode){
if (query is YamlMappingNode)
{
YamlMappingNode queryMapNode = (YamlMappingNode)query;
foreach(var item in queryMapNode.Children){
if (queryString.Length > 0)
{
queryString += "&";
}
foreach (var item in queryMapNode.Children)
{
if (queryString.Length > 0)
{
queryString += "&";
}
queryString += item.Key + "=" + (item.Value is YamlMappingNode ? item.Value.ToJson() : item.Value);
}
}
}
mock.query = queryString;
}

if (request.Children.Keys.Contains(new YamlScalarNode("header")))
{
var header = request.Children[new YamlScalarNode("header")];
var header = request.Children[new YamlScalarNode("header")];
mock.header = header.ToJson();
}

YamlScalarNode url = (YamlScalarNode)request.Children[new YamlScalarNode("url")];
if(mock.query != null && mock.query.Length > 0){
YamlScalarNode url = (YamlScalarNode)request.Children[new YamlScalarNode("url")];
if (mock.query != null && mock.query.Length > 0)
{
mock.url = MockServer.SERVER + url.Value + "?" + mock.query;
} else {
}
else
{
mock.url = MockServer.SERVER + url.Value;
}

Expand All @@ -186,16 +198,16 @@ private void ReadMockData()
}
}

private string LoadFileData(String path, String defaultString)
{
private string LoadFileData(String path, String defaultString)
{
string filePath = Path.Combine(Application.dataPath, path);
if (File.Exists(filePath))
{
// Read from file into a string
string content = File.ReadAllText(filePath);
if (File.Exists(filePath))
{
// Read from file into a string
string content = File.ReadAllText(filePath);
return content.Replace("\n", "");
}
}

return defaultString;
}
}
}
51 changes: 26 additions & 25 deletions ncmb_unity/Assets/Editor/MockServerObject.cs
Expand Up @@ -4,22 +4,20 @@
using System.Collections.Generic;
using System.Linq;

public class MockServerObject {
public class MockServerObject
{
//Variables to validate for response
public string method = "GET";
public string body = null;
public string query = null;
public string header = null;
public string url { get; set; }


public string method = "GET";
public string body = null;
public string query = null;
public string header = null;
public string url { get; set; }
//Request
public HttpListenerRequest request;

//Response data
public string responseJson = "";
public int status = 404;

public void validate()
{
//Validate method type
Expand All @@ -28,31 +26,34 @@ public void validate()
status = 404;
return;
}
//Validate Header
for (int i = 0; i < request.Headers.Count; i++)
//Validate Header
for (int i = 0; i < request.Headers.Count; i++)
{
Dictionary<string, string> headerDic = request.Headers.AllKeys.ToDictionary(t => t, t => request.Headers[t]);

if(header != null){
string pattern = @"\""(?<key>[^\""]+)\""\:\""?(?<value>[^\"",}]+)\""?\,?";
foreach (Match m in Regex.Matches(header, pattern))
{
if (m.Success)
{
if(headerDic.ContainsKey(m.Groups["key"].Value)){
if(!String.Equals(m.Groups["value"].Value, headerDic[m.Groups["key"].Value])){
if (header != null)
{
string pattern = @"\""(?<key>[^\""]+)\""\:\""?(?<value>[^\"",}]+)\""?\,?";
foreach (Match m in Regex.Matches(header, pattern))
{
if (m.Success)
{
if (headerDic.ContainsKey(m.Groups["key"].Value))
{
if (!String.Equals(m.Groups["value"].Value, headerDic[m.Groups["key"].Value]))
{
status = 404;
return;
}
} else {
}
else
{
status = 404;
return;
}
}
}
}
}
}

}

}
}

0 comments on commit 255e201

Please sign in to comment.