Skip to content

08.0 DB Sets

Amit Gupta edited this page Mar 6, 2017 · 2 revisions

DBset is the most important feature of Stubmatic. It is nothing but a data table where first row is header and other rows are actual data. Each column is separated by '|' pipe sign. There is not pipe sign expected in the starting or ending of a row. Values of first column are treated as key to identify that row.

In configuration file you need to specify dbset folder. In request-response mapping, you need to specify the name of dbset which is nothing but the file name in dbset folder.

-  request:
      url: /stubs/mix
      post: Hello ([a-zA-Z]+)!!
      method: POST
      
   dbset:
      db: Sample.txt
      key: '001'

   response:
      file: mix.txt

Sample.txt

	key	|value		|markerval
	001	|1,2		|{{TODAY}}
	1	|value	    |{{TODAY+1y-2m+3d}}

Now you can use column name as ##columnnamw## in anywhere in response body or response file.

It is a skeleton based approach. You create a response skeleton and fill it with data at run time.

Values of db and key can be dynamic.

-  request:
      url: /stubs/(admin|staff|customer|security)/([0-9]+)
      
   dbset:
      db: <% url.1 %>
      key: <% url.2 %>

   response:
      file: authentication.xml

For above example, I have created 4 dbsets: admin, staff, customer, and security. Sample data from each dbset is as below

emp_num | token
001 	 | safknf-34809n-skfnjk
002 	 | asfno0-230480932n-kjnkl
003  	 | nk002834-oknkjn-098nkjn

authentication.xml

<auth_id>##token##</auth_id>

Now if you request as localhost:7777/stubs/admin/001, it'll response <auth_id>safknf-34809n-skfnjk</auth_id>

If the url is localhost:7777/stubs/other/001, request will not match with this mapping. It'll try to match with other mappings in the file. If it matches with no mapping then it'll respond with 404.

If the url is localhost:7777/stubs/admin/004, It'll match with the above mapping. But it'll not find key value in admin dbset. It'll not try to match with further mappings in the yaml file and will respond with 404.

From stubmatic err attribute with dbset is not valid from 5.x. If key specified in mapping doesn't match with keys in specified dbset then Stubmatic mark that mapping unmatched and continue match other mappings. Similarly err.file is invalid from 5.x onwards.

However from 5.x onwards you can specified * in you dbset as default key. So if no key is matched then '' the row against '' will be picked.

Strategy

You can define strategy for dbset as well. Currently only strategy, random is supported.

-  request:
      url: /stubs/(admin|staff|customer|security)/[0-9]+
      
   dbset:
      db: authtokens
      strategy: random

   response:
      file: authentication.xml

In above example, Stubmatic will respond with random auth token.

There are more strategies in plan. So keep watching this space ... :)

See the Demo application for better understanding.

Clone this wiki locally