Coder Social home page Coder Social logo

connect-api-mocker's Introduction

connect-api-mocker

connect-api-mocker, REST API'lerle haberleşen web uygulamaları yaparken, uygulamayı REST API'nın sınırlılıklarından bağımsız olarak test edebilmek için, dosya sistemiyle sahte bir REST API sunmaya yarayan bir connect.js middleware'ıdır.

Kullanım

Bu middleware Grunt ile kolayca kullanılabilir. grunt-contrib-connect eklentisi kurulduktan sonra, konfigürasyona api-mocker middleware'i aşağıdaki gibi eklendiğinde, projenin mocks/api klasörü, /api adresinden REST servisi gibi sunulmaya başlanacaktır:

module.exports = function(grunt) {
  var apiMocker = require('./lib/middlewares/api-mocker');

  grunt.loadNpmTasks('grunt-contrib-connect');  // Connect - Gelistirme sunucusu

  // Project configuration.
  grunt.initConfig({

    // Gelistirme sunucusu
    connect: {
      server: {
        options: {
          base: './build',
          port: 9001,
          middleware: function(connect, options) {

            var middlewares = [];

            // mock/rest klasorunu sahte servis katmani olarak kullan
            middlewares.push(apiMocker(
                '/api',
                'mocks/api'
            ));

            // Statik dosyalar
            middlewares.push(connect.static(options.base));
            middlewares.push(connect.static(__dirname));

            return middlewares;
          }
        }
      }
    }
  });
}

Daha sonra grunt connect komutuyla statik sunucu çalıştırıldığında /api adresi ile mocks/api klasörünün REST API olarak sunulduğu görülecektir.

Klasör yapısı

REST servis URL'i ile aynı yapıda klasör yapısı oluşturularak, oluşturulan klasöre HTTP talep tipi ile aynı adda JSON dosyaları eklenerek bu adrese, bu tiplerde yapılan taleplere belirtilen JSON kaynaklarının gönderilmesi sağlanabilir.

Örnek REST servisi: GET /api/messages

Dosya sistemi yapısı:

_ api
  \_ messages
     \_ GET.json

Örnek REST servisi: GET /api/messages/1

Dosya sistemi yapısı:

_ api
  \_ messages
     \_ 1
        \_ GET.json

Örnek REST servisi: POST /api/messages/1

Dosya sistemi yapısı:

_ api
  \_ messages
     \_ 1
        \_ POST.json

Örnek REST servisi: DELETE /api/messages/1

Dosya sistemi yapısı:

_ api
  \_ messages
     \_ 1
        \_ DELETE.json

Hız limiti simülasyonu

api-mocker'ın 3. parametresi hız limitidir. Bu değer kilobit/saniye cinsindendir ve varsayılan olarak 0 yani limitsizdir. Düşük internet hızlarında uygulamanızın tepkisini ölçmek için bu özellik kullanılabilir.

Örnek grunt konfigürasyonu:

...
          middleware: function(connect, options) {

            var middlewares = [];

            // mock/rest klasorunu sahte servis katmani olarak kullan
            middlewares.push(apiMocker(
                '/api',
                'mocks/api',
                50          // 50 kilobit/saniye hızda gönder
            ));
...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.