<commit_before>﻿// ----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// ----------------------------------------------------------------------------

/// <reference path="../../../ZumoE2ETestAppJs/ZumoE2ETestAppJs/js/MobileServices.js" />
/// <reference path="../testFramework.js" />

function defineMiscTestsNamespace() {
    var tests = [];
    var i;
    var roundTripTableName = 'w8jsRoundTripTable';
    var paramsTableName = 'ParamsTestTable';

    tests.push(new zumo.Test('Filter does not modify client', function (test, done) {
        var client = zumo.getClient();
        var filtered = client.withFilter(function (request, next, callback) {
            throw "This is an error";
        });
        var table = client.getTable(roundTripTableName);
        table.take(1).read().done(function (items) {
            test.addLog('Retrieved data successfully: ', items);
            done(true);
        }, function (err) {
            test.addLog('Unexpected error: ', err);
            done(false);
        });
    }));

    var createLoggingFilter = function () {
        var filter = function (request, next, callback) {
            filter.request = request;
            var clientVersion = request.headers['X-ZUMO-VERSION'];
            if (clientVersion && clientVersion.length) {
                if (clientVersion[clientVersion.length - 1] == ')') {
                    clientVersion = clientVersion.substring(0, clientVersion.length - 1);
                }

                var equalsIndex = clientVersion.lastIndexOf('=');
                if (equalsIndex >= 0) {
                    clientVersion = clientVersion.substring(equalsIndex + 1);
                    zumo.util.globalTestParams[zumo.constants.CLIENT_VERSION_KEY] = clientVersion;
                }
            }

            next(request, function (error, response) {
                filter.error = error;
                filter.response = response;
                if (!error && response) {
                    var serverVersion = response.getResponseHeader('x-zumo-version');
                    if (serverVersion) {
                        zumo.util.globalTestParams[zumo.constants.SERVER_VERSION_KEY] = serverVersion;
                    }
                }

                callback(error, response);
            });
        };

        return filter;
    }

    tests.push(createFilterCaptureTest(true));
    tests.push(createFilterCaptureTest(false));

    tests.push(new zumo.Test('Filter can bypass service', function (test, done) {
        var client = zumo.getClient();
        var filter = function (request, next, callback) {
            var data = JSON.parse(request.data);<commit_msg>Check response getResponseHeader on IE9<commit_after>﻿// ----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// ----------------------------------------------------------------------------

/// <reference path="../../../ZumoE2ETestAppJs/ZumoE2ETestAppJs/js/MobileServices.js" />
/// <reference path="../testFramework.js" />

function defineMiscTestsNamespace() {
    var tests = [];
    var i;
    var roundTripTableName = 'w8jsRoundTripTable';
    var paramsTableName = 'ParamsTestTable';

    tests.push(new zumo.Test('Filter does not modify client', function (test, done) {
        var client = zumo.getClient();
        var filtered = client.withFilter(function (request, next, callback) {
            throw "This is an error";
        });
        var table = client.getTable(roundTripTableName);
        table.take(1).read().done(function (items) {
            test.addLog('Retrieved data successfully: ', items);
            done(true);
        }, function (err) {
            test.addLog('Unexpected error: ', err);
            done(false);
        });
    }));

    var createLoggingFilter = function () {
        var filter = function (request, next, callback) {
            filter.request = request;
            var clientVersion = request.headers['X-ZUMO-VERSION'];
            if (clientVersion && clientVersion.length) {
                if (clientVersion[clientVersion.length - 1] == ')') {
                    clientVersion = clientVersion.substring(0, clientVersion.length - 1);
                }

                var equalsIndex = clientVersion.lastIndexOf('=');
                if (equalsIndex >= 0) {
                    clientVersion = clientVersion.substring(equalsIndex + 1);
                    zumo.util.globalTestParams[zumo.constants.CLIENT_VERSION_KEY] = clientVersion;
                }
            }

            next(request, function (error, response) {
                filter.error = error;
                filter.response = response;
                if (!error && response && response.getResponseHeader) { // IE9 not support response.getResponseHeader
                    var serverVersion = response.getResponseHeader('x-zumo-version');
                    if (serverVersion) {
                        zumo.util.globalTestParams[zumo.constants.SERVER_VERSION_KEY] = serverVersion;
                    }
                }

                callback(error, response);
            });
        };

        return filter;
    }

    tests.push(createFilterCaptureTest(true));
    tests.push(createFilterCaptureTest(false));

    tests.push(new zumo.Test('Filter can bypass service', function (test, done) {
        var client = zumo.getClient();
        var filter = function (request, next, callback) {
            var data = JSON.parse(request.data);
{'fraction_non_alphanumeric': 0.16666666666666666, 'fraction_numerical': 0.0035536602700781805, 'mean_word_length': 1.7010556621881, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2363133', 'n_tokens_mistral': 1418, 'n_tokens_neox': 1365, 'n_words': 292}